1

I am using the objective-c json framework to parse some json from the lighthouse api - http://stig.github.com/json-framework/

I've used the framework before with other apis with no issues although i seem to have come to a dead end when trying to grab the results from the lighthouse api using JSONValue.

It appears the value is being returned incorrectly:

NSArray *results = [json_string JSONValue];

for (NSDictionary *project in results){
    NSLog(@"project found");
}

This loop only runs once although i know there are atleast 7 objects for it to itterate through in the JSON string. project is also being set as a string and not a NSDictionary, i know this as calling objectForKey on project causes an error.

[NSCFString objectForKey:]: unrecognized selector sent to instance 

I'm pretty stumped here and hope this isn't an issue with the string being returned from the Lighthouse api and i am just trying to get the contents incorrectly, my json string is here: http://pastie.org/1390233

1
  • Is that the parsed JSON (e.g., a JavaScript object) or unparsed (a string)? It looks parsed to me - what does the JSON framework expect to receive? Commented Dec 19, 2010 at 21:10

1 Answer 1

5

The parser's behavior is correct. The dictionary results contains only 1 key with the name projects which is an array. To loop through each individual project, you need to enumerate this projects property.

NSArray *projects = [results objectForKey:@"projects"];

for(NSDictionary *item in projects) {
    NSDictionary *project = [item objectForKey:@"project"];
    // now project should have the desired keys
}
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly right. It took me a bit when I first started parsing JSON to recognize that things can be nested quite deeply, e.g. Projects->Project->Results->Result. Nested enumerators, as @Anurag demonstrates, are a great way to tackle them.

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.