简体   繁体   中英

Need help using json-framework on iPhone

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.

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"];
    }
    //...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM