1

I have this JSON tree and I need to group the arrays by category key. (In this example,: "","1","2","3" and "serf" are value names for categories.) I'm serializing JSON using NSJSONSerialization but can't parse it the way I want to. Here is some of my code:

NSData *data2=[NSData dataWithContentsOfURL:url2];
result2=[NSJSONSerialization JSONObjectWithData: data2
                                        options: NSJSONReadingMutableContainers
                                          error: nil];
NSLog(@"button data: %@", result2);
for (NSDictionary *strh in [result2 objectForKey:@"template"]) {
    //the json struct above is called result2 here
    //now i need to parse any category with their names..           
}  

Related: Here is my question about how to create multidimentional arrays with key-value matching in PHP.

4

1 回答 1

2

Looking at the output, you have a dictionary with a key called template and a key called version. the object for the key template is itself a dictionary containing keys for your categories. The following code:

id foo = [[result2 objectForKey: @"template"] objectForKey: @""];

will set foo to an array containing the items with id's 52, 19, 2, 22.

Categories 1, 2 ,3 are more tricky because it's not obvious whether the keys are numbers or strings. They are probably strings, since keys in JSON have to be strings, so

id bar = [[result2 objectForKey: @"template"] objectForKey: @"1"];

will set bar to an array containing the items with id's 28, 1, 25.

于 2012-05-15T14:45:50.310 回答