I have a json collection I am able to parse perfectly fine 1 level deep, but each item in the main collection has a collection in it. I am not quite sure how to access ‘item’ to get the sub collection like I did with the main collection…
NSString *response = [request responseString];
NSDictionary *json = [response JSONValue];
NSArray *items = [json valueForKeyPath:@"item"];
for (id item in items)
{
mainObject.name = [item objectForKey:@"name"]; //this works fine
// How do I get sub collection from item?
}
Some of the json:
{"item":
{
"available_at" : null,
"created_at" : "2011-12-09T19:52:23Z",
"lo_id" : 30,
"id" : 24,
"merchant_id" : 1,
"order_id" : 25,
"reach_local_link" : null,
"status" : null,
"token" : "12-258847891-1",
"updated_at" : "2011-12-09T19:52:23Z",
"url" : "api/dir/v1/item/12-258847891-1/print",
"subitem1" :
{
"area" : "local",
"broker_id" : "",
"broker_id" : null,
"category" :
{
"category":....
In the example, there could be multiple sub items like subitem1. I need to get the collection of those and have another for loop inside the current one.
itemwill simply be a dictionary or array if it is one in the JSON object. For example, you should be able to do this:It’s simply a
NSDictionaryin there, so you can use it as one.I really recommend logging the Objective-C representation of your JSON object, it makes it easier to work with it:
Warning
It’s strongly recommended to check the type of the objects you load before assuming that they really are dictionaries, strings or arrays. If they aren’t, you’ll notice that your application is going to crash.