ios development - How to do JSON parser? -
i'm new ios development. json file server.
i use nsdictionary objects. when debug, "name", "address" values.
but don't know how can "dish" elements "recommendation" object? in java development, know recommendation object, , "dish" array element. don't know happen in ios.
{ "results": [ { "name": "ollise", "address": "columbia university", "geo": [ { "coordinates": 40 }, { "coordinates": 70 } ], "logo": "http:\/\/a0.twimg.com\/profile_images\/3159758591\/1548f0b16181c0ea890c71b3a55653f7_normal.jpeg", "recommendation": [ { "dish": "dish1" }, { "dish": "dish2" } ], } ] }
by way, in json, store url of image in "logo", cannot value when debugging. should use special format?
[restaurant setname:[dict objectforkey:@"name"]] // name [restaurant setlogo:[dict objectforkey:@"logo"]] // can nothing!
this way it
nsdictionary *response = [nsjsonserialization jsonobjectwithdata:yourdata options:nsjsonreadingmutableleaves error:nil]; nsdictionary *results = [[response objectforkey:@"results"] objectatindex:0]; nsstring *name = [response objectforkey:@"name"]; //... other objects nsarray *recommendation = [results objectforkey:@"recommendation"]; nsdictionary *recommendation1 = [recommendation objectatindex:0]; nsdictionary *recommendation2 = [recommendation objectatindex:1]; nsstring *dish = [recommendation1 objectforkey@"dish"]; nsstring *dish1 = [recommendation2 objectforkey@"dish"]; [restaurant setname:name]; nsstring *logo = [results objectforkey:@"logo"]; [restaurant setlogo:logo];
the results
same dictionary 1 in json. treat braces ({}
) nsdictionary
, brackets ([]
) nsarray
. dish
not array element, key in dictionary array element.
Comments
Post a Comment