Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Capturing "subcategory like" JSON data for iOS

Going through the JSON data capture at the moment for iOS

I notice that the JSON API for team tree house is set in such a way that it just has the category then result

what would you do if the api was to look like this..

http://m.hawkapps.co/api/get_recent_posts/

and for example I wanted to get the name section in author, would it be something like @"author:name" ?

Amit Bijlani

Thanks in advance :)

2 Answers

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

I'm assuming you already know how to parse the JSON as shown in the Blog Reader app tutorial. After having parsed the JSON and storing the data in a NSDictionary you can use the method valueForKeyPath to access child nodes.

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

    self.blogPosts = [NSMutableArray array];

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

    for (NSDictionary *bpDictionary in blogPostsArray) {
        NSString *author  = [bpDictionary valueForKeyPath:@"author.name"];
    }
Patrick Cooney
Patrick Cooney
12,216 Points

You just blew my mind. I had an "Inception" like level of objectForKey's embedded in objectForKey's.

Patrick Cooney
Patrick Cooney
12,216 Points

You can save posts in a dictionary using the json parsing methods then just drill down using a few objectForKey: methods. I did this with a weather API. It's not terribly elegant but gets the job done.