Parsing returned Json with native swift -
when hitting api blob of json back, example below
{ “item1” : 1234, “item2” : 4567, “item3” : “78910”, “item4” : “1234” }
there not structure it, want know how parse through in swift,
here bottom of code grabbing (skipped on url , request stuff)
let jsonobject: anyobject? = nsjsonserialization.jsonobjectwithdata(data, options: nil, error: nil) if (jsonobject != nil) { // process jsonresult println("\(jsonobject)"); } else { // couldn't load json, @ error }
the json prints console fine in format above, wandering how can parse on , extract each item
the whole swift json thing looks bit of mess @ moment
a more neat way it, this:
let json = "{\"item1\" : 1234, \"item2\" : 4567, \"item3\" : 78910, \"item4\" : 1234}" // simulated string response ... let data = json.datausingencoding(nsutf8stringencoding, allowlossyconversion: true) // turn nsdata if let jsonobject = nsjsonserialization.jsonobjectwithdata(data!, options: nil, error: nil) as? dictionary<string, anyobject>{ println(jsonobject["item1"]) // prints optional(1234) }else{ println("error") }
Comments
Post a Comment