ios - parsing JSON with Swift -


i'm coming android programming swift ios programming , have hard time parsing json here's string try parse :

{"response":[{"uid":111,"first_name":"somename","last_name":"somelastname","photo_100":"http:someurl/face.jpg"}]} 

here how try parse :

 if let dict = utils.convertstringtodictionary(response)! as? [string: anyobject]{        //  part 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 []")         } 

the log message gives me "not []" can't make response object. far understand, i'm doing right [string: anyobject] in "response" body of json

just in case, here's utils.convertstringtodictionary method:

public static func convertstringtodictionary(text: string) -> [string:anyobject]? {     if let data = text.datausingencoding(nsutf8stringencoding) {         {             let json = try nsjsonserialization.jsonobjectwithdata(data, options: .mutablecontainers) as? [string:anyobject]             return json         } catch {             nslog("something went wrong")         }     }     return nil } 

array in swift denotes [] dictionary in swift denotes [:] response parameter array of dictionary ... denotes [[:]] 

so parse [[string: anyobject]]

if let response = dict["response"] as? [[string: anyobject]]{      user in response{          nslog("let response \(user)")          if let first_name = user["first_name"] as? string {               nslog("first_name = \(first_name)")           }      }  } 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -