ios - How can I pull data from JSON by using swift -


i've been trying json string dictionary value, , can't work, i'm getting empty value. before tried pull data checked got json, i'm doing wrong here

let jsonresult = nsjsonserialization.jsonobjectwithdata(data, options:        nsjsonreadingoptions.mutablecontainers, error: nil) as! nsdictionary  var items = [[string:string]()] var item:anyobject  var authordictionary:anyobject  var = 0; < jsonresult["item"]?.count; i++ {                 items.append([string:string]())                 item = (jsonresult["items"] as? [[nsobject:anyobject]])!                 items[i]["content"] = item["content"] as? string                 items[i]["title"] = item["title"] as? string                 items[i]["publisheddate"] = item["published"] as? string                 authordictionary = item["author"] as! nsdictionary                 items[i]["author"] = item["displayname"] as? string } println(items) 

that's got result: [[:]]

i new json, can explain me should , did wrong?

you can iterate directly on jsonresult["items"] if has right type declared (array of dictionaries).

then inside loop have create new dictionary each time, fill dictionary data grab json response, append new dictionary items array of dictionaries:

var items = [[string:string]]()  item in jsonresult["items"] as! [[string:anyobject]] {     var newdict = [string:string]()     newdict["content"] = item["content"] as? string     newdict["title"] = item["title"] as? string     newdict["publisheddate"] = item["published"] as? string     newdict["author"] = item["displayname"] as? string     items.append(newdict) } 

as authordictionary, since it's simple dictionary , not array, if assign value in loop, overwritten each time , you'll have in end author last object.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -