views:

51

answers:

1

So I've got json-framework up and running on my project, but need help figuring out how to use it to parse this json string:

[ { "id":"0", "name":"name", "info":"This is info", "tags":[ { "id":"36", "tag":"test tag" }, { "id":"37", "tag":" tag 2" } ], "other":"nil" }, { "id":"1", "name":"name", "info":"This is info", "tags":[ { "id":"36", "tag":"test tag" }, { "id":"37", "tag":" tag 2" } ], "other":"nil" } ]

Any help and maybe sample code on how to go about this specific type of json would be great. Somehow I can't get it into a dictionary I can read out of. Thanks so much.

+5  A: 

The reason why you can't get this string into a dictionary is because it isn't a dictionary, it's an array of dictionaries

You can get the values into an Objective-C object by storing it in an NSArray:

NSArray *objects = (NSArray*) [jsonString JSONValue];

Then, you can loop over those objects that are in the array:

for(NSDictionary *dict in objects) {
    NSString *id = (NSString *) [dict objectForKey:@"id"];
    NSString *name = (NSString *) [dict objectForKey:@"name"];
    NSArray *tags = (NSArray *) [dict objectForKey: @"tags"];
    //loop over tags here...
    for(NSDictionary *tag in tags) {
        NSString *tag_id = (NSString *) [tag objectForKey:@"id"];
        NSString *tag_name = (NSString *) [tag objectForKey:@"tag"];
    }
    //...
}
Jacob Relkin
I probably can't thank you enough for showing me this. I'm really new to json parsing and all and up 'till now I've had a hell of a time finding a tutorial or anyone to help me. Thanks.
Alexander
@Alexander, you're very welcome. Anytime.
Jacob Relkin