database - JSON: read array from DB -


i need location database, it's array.

i try lot of code every code me error "could not cast value of type nsarray nsdictionary" or that.

this last try:

let jsondata:nsdictionary = nsjsonserialization.jsonobjectwithdata(urldata!, options:nsjsonreadingoptions.mutablecontainers , error: &error) as! nsdictionary var location = ((jsondata nsdictionary)["locations"] as! nsdictionary)["location_name"] as! string println(customernamedb) 

this response:

{"status":"1","city_name":"mumbai","city_id":"3","locations":[{"location_id":"1","location_name":"andheri"},{"location_id":"2","location_name":"lower parel"},{"location_id":"59","location_name":"lower parel"},{"location_id":"102","location_name":"lower parel"},{"location_id":"144","location_name":"borivali"},{"location_id":"145","location_name":"borivali"},{"location_id":"146","location_name":"borivali"},{"location_id":"147","location_name":"andheri"}]}

i need read location_name.

you're trying access array dictionary...

note: instead of stacking on same line, should break down statements , use safe unwrapping.

example:

if let jsondata = nsjsonserialization.jsonobjectwithdata(urldata!, options: nil, error: &error) as? [string:anyobject] { // dictionary     if let locationsarray = jsondata["locations"] as? [[string:anyobject]] { // array of dictionaries         locationdictionary in locationsarray { // loop in array of dictionaries             if let location = locationdictionary["location_name"] as? string { // finally, access dictionary trying                 println(location)             }         }     } } 

update swift 2.0

do {     if let jsondata = try nsjsonserialization.jsonobjectwithdata(urldata!, options: []) as? [string:anyobject] {         if let locationsarray = jsondata["locations"] as? [[string:anyobject]] {             locationdictionary in locationsarray {                 if let location = locationdictionary["location_name"] as? string {                     print(location)                 }             }         }     } } catch {     print(error) } 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -