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

Anand Altekar
Anand Altekar
1,617 Points

Blog Reader App Won't display JSON Data

I'm getting the JSon data from the link provided in the video, My log is displaying it too, but the iPhone simulator won't display it. Any help?

Anand Altekar
Anand Altekar
1,617 Points

Here is the code:

self.tableView.contentInset = UIEdgeInsetsMake(20.0f, 0.0f, 0.0f, 0.0f);

NSURL *blogURL = [NSURL URLWithString:@"http://blog.teamtreehouse.com/api/get_recent_summary/"];

NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];

NSError *error = nil;

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

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

13 Answers

Anand Altekar
Anand Altekar
1,617 Points

OK so in my cell.textlable.text I had written Title and Author instead of title and author. :P

If you get any data you should see in output.

what is the total number of posts you have set to display??

Anand Altekar
Anand Altekar
1,617 Points

umm I don't understand how I am supposed to do that..

get the code here plz

Anand Altekar
Anand Altekar
1,617 Points

Here is the entire implementation code: @interface AATableViewController ()

@end

@implementation AATableViewController

  • (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { // Custom initialization } return self; }

  • (void)viewDidLoad { [super viewDidLoad];

    self.tableView.contentInset = UIEdgeInsetsMake(20.0f, 0.0f, 0.0f, 0.0f);

    NSURL *blogURL = [NSURL URLWithString:@"http://blog.teamtreehouse.com/api/get_recent_summary/"];

    NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];

    NSError *error = nil;

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

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

}

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

pragma mark - Table view data source

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    // Return the number of sections. return 0; }

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // Return the number of rows in the section. return [self.blogPosts count]; }

  • (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:@"Author"]; return cell; }

So if u run the debugger tool, if any kind of JSON data returns null value it will not display in ios/android, so u have to first add a method to check null value... try that it might work Amit Bijlani

Anand Altekar
Anand Altekar
1,617 Points

yeah i'm getting that error when i click on it here but this is what the log displays when I run the program. Also I've copy pasted the URL many times.

2014-06-23 21:42:24.381 BlogReaderFromEmpty[464:60b] { count = 10; "count_total" = 1764; pages = 177; posts = ( { author = "Zac Gordon"; date = "2014-06-19 17:51:18"; id = 23705; thumbnail = "http://blog.teamtreehouse.com/wp-content/uploads/2014/06/pic-of-zac-150x150.png"; title = "Getting to Grips with WordPress User Roles"; url = "http://blog.teamtreehouse.com/new-course-wordpress-user-roles"; }, { author = "Hampton Paulk"; date = "2014-06-19 13:51:27"; id = 23657; thumbnail = "http://blog.teamtreehouse.com/wp-content/uploads/2014/06/laravel-150x150.jpg"; title = "Using Laravel Homestead as Your Development Environment"; url = "http://blog.teamtreehouse.com/using-laravel-homestead-development-environment"; }, { author = "Faye Bridge"; date = "2014-06-18 15:54:01"; id = 23698; thumbnail = "http://blog.teamtreehouse.com/wp-content/uploads/2014/06/CO_social-announcement-150x150.jpg"; title = "We Want to Train 10,000 Oregonians to Code!"; url = "http://blog.teamtreehouse.com/launch-code-oregon"; }, { author = "Dave McFarland"; date = "2014-06-18 14:48:41"; id = 23694; thumbnail = "http://blog.teamtreehouse.com/wp-content/uploads/2014/06/ifttt-trigger-150x150.png"; title = "Get Started Working with APIs"; url = "http://blog.teamtreehouse.com/tap-power-api"; }, { author = "Hampton Paulk"; date = "2014-06-18 09:00:53"; id = 23686; thumbnail = "http://blog.teamtreehouse.com/wp-content/uploads/2014/06/hampton-150x150.jpg"; title = "Classes and Objects in PHP"; url = "http://blog.teamtreehouse.com/classes-objects-php"; }, { author = "Jason Seifer"; date = "2014-06-17 12:31:35"; id = 23692; thumbnail = "http://blog.teamtreehouse.com/wp-content/uploads/2014/06/gscja-150x150.png"; title = "JavaScript Memory Profiling"; url = "http://blog.teamtreehouse.com/javascript-memory-profiling"; }, { author = "Amit Bijlani"; date = "2014-06-17 09:00:20"; id = 23682; thumbnail = "http://blog.teamtreehouse.com/wp-content/uploads/2014/06/Swfit-Optional-150x150.png"; title = "Understanding Optionals in Swift"; url = "http://blog.teamtreehouse.com/understanding-optionals-swift"; }, { author = "Nick Pettit"; date = "2014-06-16 12:57:00"; id = 23678; thumbnail = "http://blog.teamtreehouse.com/wp-content/uploads/2014/06/sleeping-dog-150x150.jpg"; title = "How to Stay Motivated"; url = "http://blog.teamtreehouse.com/stay-motivated"; }, { author = "Dave McFarland"; date = "2014-06-13 11:00:10"; id = 23672; thumbnail = "http://blog.teamtreehouse.com/wp-content/uploads/2014/06/dave_video-150x150.jpg"; title = "What Is Ajax and Where to Use it?"; url = "http://blog.teamtreehouse.com/what-is-ajax"; }, { author = "Gill Carson"; date = "2014-06-12 16:00:13"; id = 23648; thumbnail = "http://blog.teamtreehouse.com/wp-content/uploads/2014/06/treehouse-coding-150x150.jpg"; title = "14 of the Best Developer and Designer Job Boards"; url = "http://blog.teamtreehouse.com/14top-developer-job-boards"; } ); status = ok; }

Hmm do one thing change the count to 1 and try executing the program in emulator

Your log is displaying the whole JSON data , try changing the count to one and then execute, if it executes try trimming of the object whose value is returning null

Anand Altekar
Anand Altekar
1,617 Points

ok I don't know how to execute a program in emulator, let me look it up .

no i mean just run it in emulator

Anand Altekar
Anand Altekar
1,617 Points

ok let me change the count and try

Anand Altekar
Anand Altekar
1,617 Points

but thanks for your help :)