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.

5
  • What is the actual problem? "but can't parse it the way I want to" -- what is the result you want, and what is the result you're getting? Commented May 15, 2012 at 14:13
  • @Felixyz the result i want is to reach objects over names. for example to be able to read button data from categories Commented May 15, 2012 at 14:18
  • Without seeing the structure of result2, it's hard to help you. What are these categories you're talking about? Commented May 15, 2012 at 14:26
  • Here it is. I have shared the link at the beginning of the question Commented May 15, 2012 at 14:34
  • take a look at this please: pastebin.com/q3WuveXD Commented May 15, 2012 at 14:36

1 Answer 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.

Sign up to request clarification or add additional context in comments.

1 Comment

i'm a little confused about naming(i write myself the PHP side) but i can change. i tried somthing like this but couldnt get any result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.