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
David Brown
1,152 PointsExtra Credit JSON Google search
After completing the iOS Getting Data from the web badge, I had a go at the extra credit.
I edited the NSURL *blogURL to point at the address @"https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=JSON" and edited the objectForKey to point at one of the keys coming from the JSON data, I selected the key, "results". I then created an NSLog of dataDictionary to see if I was accessing the correct data and the google data all appears within the output window successfully.
My problem is after editing the cell.textLabel.text = [blogPost valueForKey:@""]; and selecting one of the string keys from the google JSON data and running the app I have empty cells within my app.
Help!
Thanks
7 Answers
Amit Bijlani
Treehouse Guest TeacherIf you look at the JSON data. The key "results" is within "responseData". You first need to access "responseData" and store it in a dictionary and from that "responseData" dictionary access the "results".
Amit Bijlani
Treehouse Guest TeacherYour problem is that you are not passing a key
cell.textLabel.text = [blogPost valueForKey:@""];
Try specifying a key. For example: titleNoFormatting
cell.textLabel.text = [blogPost valueForKey:@"titleNoFormatting"];
David Brown
1,152 PointsHi Amit,
Thanks for the reply. Perhaps by example was a little misleading, I am actually used the key @"results", but still don't have any cells populated with data.
Thanks
Amit Bijlani
Treehouse Guest TeacherCan you paste some more of your code? It still doesn't make a whole lot of sense because "results" will give you an array.
David Brown
1,152 PointsOf course, code below
- (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];
self.blogPosts = [dataDictionary objectForKey:@"results"];
NSLog(@"%@",dataDictionary);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *blogPost = [self.blogPosts objectAtIndex:indexPath.row];
cell.textLabel.text = [blogPost valueForKey:@"title"];
cell.detailTextLabel.text = [blogPost valueForKey:@"visibleUrl"];
return cell;
}
Thanks
David Brown
1,152 PointsAs "cursor" and "results" are not contained within square brackets which I understood to indicate an array, I thought I'd be able to access the "results" key directly as with "posts" from the treehouse JSON data, obviously not. I'll give it a go and let you know how I get on.
Thanks again
Benjamin Harrison
4,784 PointsI was having the exact same trouble (base problem and confused by lack of [] as well) and your explanation worked for me.
Thanks, Amit.