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
Chris Bedoya
758 PointsDisplaying 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
Aaron Daub
Courses Plus Student 96 PointsNSDictionary* 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]];
Chris Bedoya
758 PointsI added this to my UpcomingReleaseViewController.m
Chris Bedoya
758 PointsChris Bedoya
758 PointsI added this to my UpcomingReleaseViewController.m
But I get an error saying that No Known class method for selector 'objectAtIndex'
Here I get an error saying Use of undeclared identifier URLString
Aaron Daub
Courses Plus Student 96 PointsAaron Daub
Courses Plus Student 96 PointsWhere are you declaring JSONObject?
Aaron Daub
Courses Plus Student 96 PointsAaron Daub
Courses Plus Student 96 PointsI 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.