【问题标题】:How to get the String Value in JSON如何在 JSON 中获取字符串值
【发布时间】:2016-01-27 13:34:04
【问题描述】:

这是我的 JSON

{
    "StatusResponse": 
    {
        "StatusCode": "000"
        "StatusDescription": "Operation Success(000)"
        "DebugDescription": "OperationSuccess"
    }-
    "memId": "3e369fec-a9c5-418b-a950-0647f7e15d7c"
    "token": null
    "isAdmin": false
    "isTeacher": false
    "isParent": true
    "kinderId": null
}

我可以使用Http Get方法检索StatusResponse的字典。

RestApiManager.sharedInstance.makeGetRequest("myURL", onCompletion: {json in
        for result in json["StatusResponse"].dictionaryValue
        {
            print(result)
        }
    })

在这里,我的结果输出会是这样的。

("DebugDescription", OperationSuccess)
("StatusDescription", Operation Success(000))
("StatusCode", 000)

我想问的问题是如何使用 FOR EACH 循环在我的代码中获取 StatusCode 的 stringValue?

【问题讨论】:

  • OP,您使用的是 SwiftyJSON,在您的问题中提及它会是一件好事...
  • json["StatusResponse"]["StatusCode"]?
  • 我的答案对你有用。勾选并投票我的答案。它对其他人有帮助。

标签: ios json swift rest get


【解决方案1】:

使用这个:

if let statusCode = json["StatusResponse"]["StatusCode"].string {
    println(statusCode)
}

【讨论】:

  • @Ramest_T 如果我想为每个循环使用怎么办?
  • 它将打印完整的字典。如果您想从响应中获得特定值。使用 yourResponseDict["requiredKey"]。
【解决方案2】:
   let statusCodeValue = json["StatusResponse"]["StatusCode"].stringValue
   print("The ststusCodeValue is - \(stausCodeValue)")

   let tatusDescriptionValue = json["StatusResponse"]["StatusDescription"].stringValue
   print("The statusDescriptionValue is - \(statusDescriptionValue)")

   let debugDescriptionValue = json["StatusResponse"]["DebugDescription"].stringValue
   print("The debugDescriptionValue is - \(DebugDescriptionValue)")

【讨论】:

  • 我以前也这样做过,但不幸的是它不起作用...错误在这里=>类型'(String,JSON)'没有下标成员
  • Type '(String, JSON)' has no subscript members...这是Xcode显示的错误
猜你喜欢
  • 2017-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-30
  • 2021-07-13
  • 1970-01-01
  • 2015-03-22
  • 1970-01-01
相关资源
最近更新 更多