0

I am trying to parse JSON in objective-c but am having trouble. The example in the tutorial I am following only goes to the first level after the parent node. I am trying to get data that is a bit deeper. Any advice on how to do this?

The elements I am trying to get: Title: data.children[i].data.title Thumbnail: data.children[i].data.thumbnail Json: http://www.reddit.com/r/HistoryPorn/.json

NSURL *blogURL = [NSURL URLWithString:@"http://www.reddit.com/r/HistoryPorn/.json"];
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];

NSError * error = nil;


NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];

self.blogPosts = [NSMutableArray array];

NSArray * blogPostsArray = [dataDictionary objectForKey:@"data"];

for (NSDictionary *bpDictionary in blogPostsArray) {
    BlogPost * blogPost = [BlogPost blogPostWithTitle:[bpDictionary objectForKey:@"title"]];
    blogPost.thumbnail = [bpDictionary objectForKey:@"thumbnail"];
    blogPost.url = [NSURL URLWithString:[bpDictionary objectForKey:@"url"]];
    [self.blogPosts addObject:blogPost];
}
2
  • What kind of trouble do you have? Would you describe the problem/error? Commented Jun 21, 2013 at 22:42
  • I'm not able to get to the the thubmnail/url points because they are nested deeper into the JSON. How do I get deeper into the JSON? Commented Jun 21, 2013 at 22:43

1 Answer 1

0

With the new syntax it should be easier to gets keys in a nested dictionaries. You can know the full keys/indexes path by just drawing a tree, remember that a dictionary starts with braces, and an array starts with brackets. For example let's retrieve the "thumbnail" and "url" value for the first entry in the children array:

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
if(!json)
{
    // Always handle eventual errors:
    NSLog(@"%@",error);
    return;
}
NSString* thumbnail= json[@"data"][@"children"][0][@"data"][@"thumbnail"];
NSString* url= json[@"data"][@"children"][0][@"data"][@"url"];
Sign up to request clarification or add additional context in comments.

3 Comments

Remark: One should check the return value: if (json == nil), not if (error).
The method returns nil just if the method fails. In case it fails, it produces a NSError object, so I think it's equivalent.
From developer.apple.com/library/ios/#documentation/cocoa/conceptual/…: "When dealing with errors passed by reference, it’s important to test the return value of the method to see whether an error occurred, as shown above. Don’t just test to see whether the error pointer was set to point to an error." - This might be a theoretical issue, but strictly speaking the error might be != nil if the function succeeds.

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.