1

I'm coming from android programming to Swift iOS programming and have a hard time parsing a json here's String I try to parse :

{"response":[{"uid":111,"first_name":"someName","last_name":"someLastName","photo_100":"http:someUrl/face.jpg"}]}

here how I try to parse this :

 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 []")
        }

the Log message gives me "not an []" as it can't make a response object. As far as I understand, I'm doing right as [String: AnyObject] is what is in "response" body of my json

Just in case, here's my Utils.convertStringToDictionary method:

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
}
1
  • 1
    your "response" parameter is array not dictionary Commented Jul 28, 2016 at 11:11

3 Answers 3

3
Array in swift denotes with []
Dictionary in swift denotes with [:]
your response parameter is array of dictionary ... so it denotes with [[:]]

so just parse it with [[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)")
          }
     }
 }
Sign up to request clarification or add additional context in comments.

3 Comments

could you please give me an explanation on this double square braces in [[String: AnyObject]] ?
Thanx a lot! so, response is actually an array of dictionaries, right?
yes ... in this answer.. I did explain what brackets, braces in JSON suggest what ... it may be helpful to you
2

Problem here is response is an array

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

2 Comments

can you tell me how to parse it then? I kind of confused as in fact it's a HashMap<String, Object> in terms of Java , but here it's an array - how do I get Value for Key?
i've added my answer and added parsed example
0

Try this code

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 []")
     }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.