Ios 用Swift解析JSON

Ios 用Swift解析JSON,ios,json,swift,parsing,Ios,Json,Swift,Parsing,我从android编程到Swift iOS编程,很难解析json 下面是我试图解析的字符串: {"response":[{"uid":111,"first_name":"someName","last_name":"someLastName","photo_100":"http:someUrl/face.jpg"}]} 下面是我如何尝试分析这个: if let dict = Utils.convertStringToDictionary(response)! as? [String: Any

我从android编程到Swift iOS编程,很难解析json 下面是我试图解析的字符串:

{"response":[{"uid":111,"first_name":"someName","last_name":"someLastName","photo_100":"http:someUrl/face.jpg"}]}
下面是我如何尝试分析这个:

 if let dict = Utils.convertStringToDictionary(response)! as? [String: AnyObject]{
       //  this part is yet doing ok
       if let response = dict["response"] as? [String: AnyObject]{
           NSLog("let response \(response)")
           if let first_name = response["first_name"] as? String {
                NSLog("first_name = \(first_name)")
           }
        }
        else {
            NSLog("not an []")
        }
日志消息给我“不是[]”,因为它无法生成响应对象。据我所知,我做得不错,因为
[String:AnyObject]
是json的“response”主体

以防万一,下面是我的Utils.convertStringToDictionary方法:

public static func convertStringToDictionary(text: String) -> [String:AnyObject]? {
    if let data = text.dataUsingEncoding(NSUTF8StringEncoding) {
        do {
            let json = try NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers) as? [String:AnyObject]
            return json
        } catch {
            NSLog("Something went wrong")
        }
    }
    return nil
}

这里的问题是响应是一个
数组

if let response = dict["response"] as? NSArray{
   for value in response as? NSDictionary{
      print(value["uid"]) /// prints 111
      print(value["first_name"]) /// prints someName
   }
}

这里的问题是响应是一个
数组

if let response = dict["response"] as? NSArray{
   for value in response as? NSDictionary{
      print(value["uid"]) /// prints 111
      print(value["first_name"]) /// prints someName
   }
}
所以只要用
[[String:AnyObject]]

if let response = dict["response"] as? [[String: AnyObject]]{
     for user in response{
         NSLog("let response \(user)")
         if let first_name = user["first_name"] as? String {
              NSLog("first_name = \(first_name)")
          }
     }
 }
所以只要用
[[String:AnyObject]]

if let response = dict["response"] as? [[String: AnyObject]]{
     for user in response{
         NSLog("let response \(user)")
         if let first_name = user["first_name"] as? String {
              NSLog("first_name = \(first_name)")
          }
     }
 }
试试这个代码

if let dict = Utils.convertStringToDictionary(response)! as? [String: AnyObject]{
      //  this part is yet doing ok
      if let response = dict["response"] as? NSArray{
         NSLog("let response \(response)")

         for dict in response{

             if let first_name = dict["first_name"] as? String {
                 NSLog("first_name = \(first_name)")
             }
         }                      

      }
      else{
           NSLog("not an []")
     }
}
试试这个代码

if let dict = Utils.convertStringToDictionary(response)! as? [String: AnyObject]{
      //  this part is yet doing ok
      if let response = dict["response"] as? NSArray{
         NSLog("let response \(response)")

         for dict in response{

             if let first_name = dict["first_name"] as? String {
                 NSLog("first_name = \(first_name)")
             }
         }                      

      }
      else{
           NSLog("not an []")
     }
}

你的“response”参数是数组而不是字典你的“response”参数是数组而不是字典你能告诉我如何解析它吗?我有点困惑,因为事实上它是Java中的一个HashMap,但这里是一个数组-我如何获得键的值?我已经添加了我的答案并添加了已解析的示例。您能告诉我如何解析它吗?我有点困惑,因为事实上它是Java中的一个HashMap,但这里是一个数组-我如何获得键的值?我已经添加了我的答案并添加了解析的示例。你能给我解释一下[[String:AnyObject]]中的这个双方括号吗?Thanx很多!所以,响应实际上是一组字典,对吗?是的。。。在这个。。我确实解释了JSON中的括号和大括号表明了什么。。。这可能对你有帮助,你能给我解释一下[[String:AnyObject]]中的这个双方括号吗?谢谢!所以,响应实际上是一组字典,对吗?是的。。。在这个。。我确实解释了JSON中的括号和大括号表明了什么。。。这可能对你有帮助