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

Stuck on JSON Extra Credit getting data from Google search

Working on the extra credit in the Getting Data from the Web. The assignment - Extra Credit JSON Google Search

Use the Google JSON Search API URL to request JSON search results from Google: https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=JSON

The last part of the URL after "q=" is the search term. Switch to use this URL and display the results.

I get the feed in to my dictionary, NSLog confirms however I'm somehow not getting the dictionary data with key in to the array which feeds the tableview. I've tested my code which is largely verbatim from the Amit's last BlogReader project. My code processes the JSON from Treehouse and a couple other feeds I found but when trying to process the Google search I get nothing in the table. My hunch is I'm hitting a JSON key that is null. My main m file follows. Thanks to whoever may look at this. I'll break through eventually but I think the fix is getting the process to skip a key and then process.

// // MasterViewController.m // BlogReaderXtra // // Created by Admin on 3/24/15. // Copyright (c) 2015 Wood and Wire Ware. All rights reserved. //

import "TableViewController.h"

import "DetailViewController.h"

@interface TableViewController ()

@property NSMutableArray *objects; @end

@implementation TableViewController

  • (void)awakeFromNib { [super awakeFromNib]; }

  • (void)viewDidLoad { [super viewDidLoad]; NSURL *googleURL = [NSURL URLWithString:@"https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=JSON"];

    NSData *jsonData = [NSData dataWithContentsOfURL:googleURL];

    NSError *error = nil; NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:// note I have tried nothing here and I have tried method - NSJSONReadingAllowFragments// error:&error];

    self.googleJSONResults = [dataDictionary objectForKey:@"results"]; NSLog(@"%@",dataDictionary); }

  • (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }

pragma mark - Table View

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; }

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.googleJSONResults.count; }

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; NSDictionary *dataDictionary1 = [[NSDictionary alloc] init]; dataDictionary1 = [self.googleJSONResults objectAtIndex:indexPath.row]; NSString *title = [dataDictionary1 objectForKey:@"visibleUrl"]; // choosing two keys in the JSON feed NSString *sub = [dataDictionary1 objectForKey:@"titleNoFormatting"]; // above and here

    cell.textLabel.text = title; cell.detailTextLabel.text = sub; return cell; } @end

3 Answers

I think that your problem is that you don't access the right key for the first time. If you pay a little attention your JSON have three keys at the first level : responseData, responseDetails and responseStatus. So when you try to assign self.JSONResults the value of [dataDictionary objectForKey:@"results"]; it will be nil since it won't find any key named "results" in your JSON. Try instead
NSDictionary *dictionary = (NSDictionary *)[dataDictionary objectForKey:@"responseData"]; self.JSONResults = [dictionary objectForKey@"results"];. Tell me if it worked.

Lulian thanks very much for the input. That's the exact hole I saw that I was somehow hitting the wrong key though comparing to other JSON script the pattern looked right which may have been a naive misconception on my part. I tried responseData as the OFK earlier but it looks like you have pointed me a little differently with it which I will try and let you know but thank you again for taking the time.

Lulian thank you again for pointing me the way. I applied your lead as follows and it was successful. For bringing select keys in to the tableview for the label/subtitle I chose title and visibleUrl. NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error]; NSDictionary *dictionary = [dataDictionary objectForKey:@"responseData"]; self.googleJSONResults = [dictionary objectForKey:@"results"]; NSLog(@"%@",dictionary);

Worked like charm. What seems to be going on and correct me but the original dictionary is now setting a new boundary and capturing the nil key then the new dictionary can get to results. I was originally trying responseData in the same place but it was crashing and I gather that's because that key was nil if I understand this correctly? The second dictionary allows for a new start point and the key to the fields I need, is that sort of the idea?