0

I am retrieving json data from a url. The json "string" has one object that contains multiple objects inside of it, each of of the objects containing their own key, value properties...essentially the json object is an NSArray containing objects that are more or less dictionary objects. I'm trying to use NSJSONSerialization but there isn't a lot of documentation on it, and I can't seem to figure out how to Here is an example of what the json might look like:

{"allObjects":[{"firstName":"homer","middleName":"j","lastName":"simpson"
,"address": "123 evergreen terrace","countryCode":"US","zip":12345},
{"firstName": "marge","middleName":"b","lastName":"simpson","address":
"123 evergreen terrace","countryCode":"US","zip":12345}]}

(If it matters, the JSON object contains MANY more entries)

Are there specific "options" I need to use with NSJSONSerialization to get an array of dictionaries out (the array being "allObjects")?

Any help would be greatly appreciated!

Thanks in advance.

UPDATE

This code results in an error:

@vishy I thought this too, but when I run this code,

NSArray *json = [NSJSONSerialization JSONObjectWithData:geoNamesJSON options: kNilOptions error:&error];
NSDictionary *dict = [json objectAtIndex:0];
NSArray *allKeys = [dict allKeys];
NSString *key = [[NSString alloc] init];
NSEnumerator *keyEnum = [allKeys objectEnumerator];
NSLog(@"All keys in the first dictionary object:");
while(key = [keyEnum nextObject]){
    NSLog(@"%@", key);}

I get this error:

-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x1fd7cd10 * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x1fd7cd10' * First throw call stack: (0x3796c2a3 0x35c7c97f 0x3796fe07 0x3796e531 0x378c5f68 0xf310b 0xf219d 0x38ca4321 0x38cdecfd 0x38d53cc1 0x38cd86a7 0x38cd8481 0x38c52abb 0x38cc78d7 0x355b9bd9 0x3665c4b7 0x366611bd 0x3793ff3b 0x378b2ebd 0x378b2d49 0x37def2eb 0x38c91301 0xd3551 0x3c39db20) libc++abi.dylib: terminate called throwing an exception (lldb)

2
  • in the given json- it is a dictionary with one object within it their is an array of 2 dictionaries.. to parse any json u should be knowing the format of ur json & then only u can write ur parsing code.. Commented Oct 21, 2012 at 14:04
  • @vishy I updated my question (can't figure out the mini-Markdown) with the issue using this method. Commented Oct 21, 2012 at 14:26

1 Answer 1

1

Are you getting any values in array after doing this step

NSArray *json = [NSJSONSerialization JSONObjectWithData:geoNamesJSON options:kNilOptions error:&error]; 

i dont think so, as your json is a dictionary. It should be

NSDictionary* json = [NSJSONSerialization JSONObjectWithData:geoNamesJSON options:kNilOptions error:&error];

For info on handling json you can follow this tutorial.

Edit: Parse the data in this way after getting the json dictionary using above lines

NSArray* geoList = [geoDict objectForKey:@"geonames"];
NSLog(@"first object in geolist--%@",[geoList objectAtIndex:0]);
//each object in the array is a dictionary
Sign up to request clarification or add additional context in comments.

15 Comments

as u get dictionary after serialization u can print & check in which format is it.. can u show dat log..?
geonames = ( { adminCode1 = OMITTED adminName1 = OMITTED countryCode = OMITTED countryName = OMITTED distance = "OMITTED"; fcl = OMITTED; fclName = "OMITTED"; fcode = OMITTED; fcodeName = OMITTED; geonameId = OMITTED; lat = "OMITTED"; lng = "OMITTED"; name = "OMITTED"; population = 0; toponymName = OMITTED; });
what are '=' here.. & wat is 'geonames' according to this the json has only one dictionary, which as strings(objects) & keys..
I simply did as you asked. First I did: NSDictionary *json = [NSJSONSerialization JSONObjectWithData:geoNamesJSON options: kNilOptions error:&error]; NSLog(@"%@", json);
i have some doubts... check my comments above
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.