Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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