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

Displaying an Image through JSON

Following the BlogReader App example how can I show a thumbnail image when my JSON is nested:

upcoming_releases: [
    {
            id: 2,
            release_name: "Lebron X Low",
            release_price: "165",
            release_colorway: "Raspberry-Red/Blueprint-Court",
            release_date: "2013-09-07T00:00:00.000Z",
            url: "http://obscure-lake-7450.herokuapp.com/upcoming/2",
            images: [
            {
                image_file: {
                    image_file: {
                        url: "https://s3.amazonaws.com/soleresource/uploads/releases/nike-lebron-x-low-raspberry.jpg",
                    thumb: {
                        url: "https://s3.amazonaws.com/soleresource/uploads/releases/thumb_nike-lebron-x-low-raspberry.jpg"
                }
                image_file: {
                    image_file: {
                        url: "https://s3.amazonaws.com/soleresource/uploads/releases/nike-lebron-x-low-raspberry-2.jpg",
                    thumb: {
                        url: "https://s3.amazonaws.com/soleresource/uploads/releases/thumb_nike-lebron-x-low-raspberry-2.jpg"
                }               
            }
        }
    },

I want to show the Image from the FIRST thumb

Thanks.

2 Answers

NSDictionary* JSONObject = ... // serialize JSON
NSString* URLString = [[[[[[[JSONObject objectForKey:@"upcoming_releases"] objectAtIndex:0] objectForKey:@"images"] objectAtIndex:0] objectForKey:@"image_file"] objectForKey:@"thumb"] objectForKey:@"url"];
UIImage* image = [UIImage imageWithContentsOfURL:[NSURL URLWithString:URLString]];

I added this to my UpcomingReleaseViewController.m

for (NSDictionary *bpDictionary in blogPostsArray) {
    BlogPost *blogPost = [BlogPost blogPostWithReleaseName:[bpDictionary objectForKey:@"release_name"]];
    blogPost.thumb = [[[[[[JSONObject objectAtIndex:0] objectForKey:@"images"] objectAtIndex:0] objectForKey:@"image_file"] objectForKey:@"thumb"] objectForKey:@"url"];
    [self.blogPosts addObject:blogPost];
}

But I get an error saying that No Known class method for selector 'objectAtIndex'

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    BlogPost *blogPost = [self.blogPosts objectAtIndex:indexPath.row];

    if ( [blogPost.thumb isKindOfClass:[NSString class]]) {
        NSData *imageData = [NSData dataWithContentsOfURL:blogPost.thumbnailURL];
        UIImage *image = [UIImage imageWithContentsOfURL:[NSURL URLWithString:URLString]];;

        cell.imageView.image = image;
    } else {
        cell.imageView.image = [UIImage imageNamed:@"treehouse.png"];
    }    

Here I get an error saying Use of undeclared identifier URLString

Where are you declaring JSONObject?

I edited my code. I'm now accessing the top-level JSON object (a dictionary) and getting the object at the @"upcoming_releases" key. After that the code is as before.

I added this to my UpcomingReleaseViewController.m