ios - Objective C how to read from nested JSON -


i have read values nested json , can't read them properly. json looks this:

 "addons" : [       {         "group_title" : "veggie toppings",         "group_type" : "t",         "item_id" : "29",         "addon" : [           {             "id" : "31",             "item_title" : "ham"           },           {             "id" : "32",             "item_title" : "mushrooms"           }         ]       },       {         "group_title" : "meat toppings",         "group_type" : "t",         "item_id" : "33",         "addon" : [           {             "id" : "30",             "item_title" : "sausage"           }         ]       } 

and code is:

-(void)setdata:(nsmutabledictionary *)menuitems{     self.menuitem = menuitems;     // [gsdvactivityindicator stopwithid:knotificationhello];     self.dictionaryorder = [nsmutabledictionary dictionarywithdictionary:[self.menuitem valueforkey:@"order"]];     self.arrayprices = [nsarray arraywitharray:[self.menuitem valueforkey:@"price"]];     self.arrayaddons = [nsarray arraywitharray:[self.menuitem valueforkey:@"addons"]];     self.myaddons = [nsarray arraywitharray:[self.arrayaddons valueforkey:@"addon"]];      [self setviewguidata];      nslog ( @"addons= %@", [self.arrayaddons valueforkey:@"group_title]);      nslog ( @"addon = %@", [self.myaddons valueforkey:@"item_title"]);  } 

and result is:

2014-03-24 19:48:04.446 [3698:70b] addons= (     "veggie toppings",     "meat toppings" ) 2014-03-24 19:48:04.447 [3698:70b] addon = (         (         ham,         mushrooms     ),         (         "sausage"     ) ) 

so problem in second part items "ham, mushrooms , sausage" in bad format , can't read them , put them in label. line bad :

self.myaddons = [nsarray arraywitharray:[self.arrayaddons valueforkey:@"addon"]]; 

but don't know how correct it.

thanks.

what you're doing working correctly. values "addon" [{"id" : "31", "item_title" : "ham"}, {"id" : "32", "item_title" : "mushrooms"}] , {"id" : "30","item_title" : "sausage"}, valueforkey giving array containing 2 values.

you don't format want, i'm guessing want flat list. can make array, iterate on groups, , put in each addon:

nsmutablearray* addons = [nsmutablearray array]; nsarray* groups = [self.menuitem valueforkey:@"addons"]; [groups enumerateobjectsusingblock:^(nsdictionary* group, nsuinteger idx, bool *stop) {     nsarray* addonsinthisgroup = group[@"addon"];     [addonsinthisgroup enumerateobjectsusingblock:^(nsdictionary* addon, nsuinteger idx, bool *stop) {         [addons addobject:addon];     }]; }]; 

this result in:

[     {"id" : "31", "item_title" : "ham"},      {"id" : "32", "item_title" : "mushrooms"},      {"id" : "30","item_title" : "sausage"} ] 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -