ios - Getting a Value from empty Key in NSArray / NSDictionary -
i need take out value json. json value goes this:
[{"status":1,"data":[{"":558202}]}] i have tried several method posted in stackoverflow. still not getting desire reuslt.
the nearest result close getting
( 558202 ) but need 558202. have tried
nsarray *jsonobject = [nsjsonserialization jsonobjectwithdata:[resultstr datausingencoding:nsutf8stringencoding]options:0 error:null]; nsdictionary *tabledata = [jsonobject valueforkey:@"data"]; nsstring *tempdata=[tabledata objectforkey:@""]; while above method error:
-[__nsarrayi objectforkey:]: unrecognized
the object key data not dictionary, it's array containing 1 dictionary.
the error message does, in fact, tell that.
--[__nsarrayi objectforkey:] : unrecognized ^^^^^^^^ subclass of nsarray you need
nsarray *jsonobject = [nsjsonserialization jsonobjectwithdata:[resultstr datausingencoding:nsutf8stringencoding]options:0 error:null]; nsarray *tabledata = [[jsonobject objectatindex: 0] objectforkey:@"data"]; nsstring *tempdata=[[tabledata objectatindex: 0] objectforkey:@""]; nb reason valueforkey: worked on array because, arrays, applies valueforkey: every element in array , returns array results in.
edit
the above code can written
nsstring *tempdata = jsonobject[0][@"data"][0][@""]; using modern syntax. rmaddy says below, don't chain things because element in json collection can json compatible type, in real life have check kind of object got e.g.
id jsonobject = [nsjsonserialization jsonobjectwithdata:[resultstr datausingencoding:nsutf8stringencoding]options:0 error:null]; if ([jsonobject iskindofclass: [nsarray class]]) { id firstelement = jsonobject[0]; if ([firstelement iskindofclass: [nsdictionary class]]) { id data = firstelement[@"data"]; // etc } } else { // errror }
Comments
Post a Comment