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 Build a Blog Reader iPhone App Getting Data from the Web Downloading and Parsing JSON Data

Yusuf Afghan
Yusuf Afghan
4,929 Points

Having trouble displaying Google Search Results

I'm doing the extra credit one where you have to display the search results. I've kept the same variable names like blog posts even though it doesn't make much sense keeping them the same! I've seen that [self.blogPosts Count] returns nil and tableView: CellForRowAtIndexPath isn't even called. What have I done wrong? Here's the URL I'm using: https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=JSON And here's the viewDidLoad method:

  • (void)viewDidLoad { [super viewDidLoad];

    NSURL *blogURL = [NSURL URLWithString:@"https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=JSON"];

    NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];

    NSError *error = nil;

    NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error]; NSLog(@"%@",dataDictionary);

    self.blogPosts = [dataDictionary objectForKey:@"results"]; }

2 Answers

Stone Preston
Stone Preston
42,016 Points

the JSON looks like this:

{"responseData": {"results": ...

you need to assign the response data to a dictionary first, then access the results key of that dictionary:

NSDictionary *responseData = [dataDictionary objectForKey:@"responseData"];
self.blogPosts = [responseData objectForKey:@"results"]; 
Yusuf Afghan
Yusuf Afghan
4,929 Points

Thankyou! That worked!

Stone Preston
Stone Preston
42,016 Points

awesome glad I could help