Reputation:
How can I consume this JSon data in iOS 5
({assets = ( { identity = 34DL3611;}, {identity = 34GF0512;}, {identity = 34HH1734;}, {identity = 34HH1736;}, {identity = 34YCJ15;} );
identity = DEMO;})
getting this data on console through this call
id list =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
NSLog(@"VLIST: %@", list);
Now I have got the data in exact JSON format after using encoding:NSUTF8StringEncoding, I want to use native jsonserializer of iOS 5
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
the JSON data is:
[{"assets":[{"identity":"34DL3611"},{"identity":"34GF0512"},{"identity":"34HH1734"},{"identity":"34HH1736"},{"identity":"34YCJ15"}],"identity":"DEMO"}]
Now how can I get this data, so that I would get assets array values and populate them in table and get the value of identity(which is DEMO) to use it as a header.
thanx
Upvotes: 0
Views: 1212
Reputation: 78795
It seems that you are able to successfully parse the JSON data and now would like to know how you can access the data. The parsed JSON data is either a NSDictionary
or a NSArray
instance containing NSDictionary
, NSArray
, NSString
, NSNumber
etc. instances.
From your sample data, it looks as if your data is heavily nested. (The purpose isn't quite clear.) It's an array containing a dictionary containing an array containing a dictionary.
You could access it like this:
NSArray list =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
NSDictionary dict = [list objectAtIndex: 0];
NSArray assets = [dict objectForKey: @"assets"];
NSString identity = [dict objectForKey: @"identity"];
for (NSUInteger index = 0; index < [assets count]; index++) {
NSDictionary itemDict = [assets objectAtIndex: index];
NSString itemIdentity = [itemDict objectForKey: @"identity"];
}
Upvotes: 2
Reputation: 1207
SBJson is another option to consider.
SBJsonParser* parser = [[SBJsonParser alloc] init];
NSDictionary* dict = [parser objectFromString:jsonString];
[parser release];
It's API is very handy and formatted very much like Apple's Class References.
Upvotes: 0
Reputation: 6710
JSONKit has better performance and is very easy to add to your project (2 files) and easy to use.
NSDictionary *dict = [myJsonString objectFromJSONString];
When I'm working with json data, I always run it through an online json formater, parser and validator. This lets me know that the json is valid and improves my understanding of the data.
Upvotes: 1