Alex
Alex

Reputation: 2513

Problem getting JSON contents with JSONValue

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

Upvotes: 1

Views: 2207

Answers (1)

Anurag
Anurag

Reputation: 141899

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
}

Upvotes: 5

Related Questions