Search code examples
objective-cjsonjson-framework

Trouble getting results from json message in objective c using JSON.Framework


I am attempting to read a json message using the json.framework. The message is a nested collection of meeting details. My desire is to iterate thought all of the meetings and create local meeting objects with the details read from the message. I see to get list of the 15 meetings that are in the json results but can't get the individual values from the results.

Here is my sample code. I am using a file for the json message so that I don't have to involve the server in this test. The json message can be downloaded here.

-(void)TestParse:(NSString *)response
{
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"conference_calls" ofType:@"json"];
    NSString *fileContent =[[NSString alloc]initWithContentsOfFile:filePath];

    parser = [SBJsonParser new];
    NSArray *results = [parser objectWithString:fileContent];

    NSLog(@"Number of itmems in the results: --> %i", [results count]);

    for(NSDictionary *conf in results){

        //Load local objects with the values of the Conf info.

        NSLog(@"This the description %@ ",[c valueForKey:"phone_number"]);

        NSLog(@"Number of Items in Dic: %i",[conf count]);

        NSLog(@"File contents: %@",[conf description]);
    }

Solution

  • The structure of your json is an array of dictionaries. But each dictionary has just a single key called "conference_call", with the value for that key being another dictionary with all the details of that call.

    So something like this should work:

    for (NSDictionary* call in results) {
    
        // get the actual data for this call
        NSDictionary *callDetails = [call objectForKey:@"conference_call"];
    
        NSLog (@"Location is %@", [callDetails objectForKey:@"location"]);
    }
    

    Hope that helps.