iosobjective-cjsonjsonkit

Parse JSON in Objective C


I have this json returned from a cms and I need to break it apart and use the different values.

{"nodes":
     [{"node":
        {"created":"14012013165204","lastupdated":"03042013133901","type":"News"}
     }]
}

I use JSONKit framework and here is my code:

    NSString* theURL = [NSString stringWithFormat:@"http://testserver.com/content_lastupdate.json"];
    NSError* err = nil;
    NSURLResponse* response = nil;
    NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init];
    NSURL*URL = [NSURL URLWithString:theURL];
    [request setURL:URL];
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    [request setTimeoutInterval:30];
    NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

    NSString *output = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSDictionary *resultsDictionary = [output objectFromJSONString];

    NSLog([NSString stringWithFormat:(@"Response: %@"), output]);

    NSDictionary *nodes = [resultsDictionary objectForKey:@"nodes"];
    NSObject *node = [nodes objectForKey:@"node"];

I do not know how to get the next part of the results. nodes key is found but the next one is not. Any idea what basics I'm missing here?


Solution

  • "nodes" is an array. Try this one:

    NSArray *nodes = resultsDictionary[@"nodes"];
    NSDictionary *node = nodes[0];
    node = node[@"node"]