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

Parsing JSON Data - iOS

Hello,

I watched all the lecture from "Getting Data from the Web".

Now i am trying to load a Youtube playlist on a UITableView.

Here is the playlist :

https://gdata.youtube.com/feeds/api/playlists/PL85E8F3CC22BCEDC6?v=2&alt=jsonc

I managed to get all the titles of the videos on NSArray and i can view the array on the console. But for some reason i get an error when i try to get the tiltes on the UITable.

I am not sure where the problem is. Please have a look at my code:

- (void)viewDidLoad
{
    [super viewDidLoad];


    NSURL *blogURL = [NSURL URLWithString:@"https://gdata.youtube.com/feeds/api/playlists/PL85E8F3CC22BCEDC6?v=2&alt=jsonc"];

    NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];

    NSError *error = nil;

    NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];


    NSDictionary *data = [dataDictionary objectForKey:@"data"];

    NSDictionary *item = [data objectForKey:@"items"];

    for (NSDictionary *video in item) {
        NSDictionary *title = [video objectForKey:@"video"];
        NSArray *titleArray = [title objectForKey:@"title"];

          NSLog(@"%@",titleArray);

          self.blogPosts = [title objectForKey:@"title"];


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

- (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 1;
}

- (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];

    NSArray *blogPost = [self.blogPosts objectAtIndex:indexPath.row];

    cell.textLabel.text = [blogPost objectAtIndex:indexPath.row];
    //cell.detailTextLabel.text = [blogPost valueForKey:@"author"];
    return cell;
}

2 Answers

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

@Tauseef

I think your misinterpreting the data. An object within a JSON file is only considered an Array if it enclosed within [ ] (box brackets.

This line in your code:

NSArray *titleArray = [title objectForKey:@"title"];

Suggests that you are assuming the key title to return an array which is not the case. The title key simply returns a string.

You need to store 'items' in an array because it is in array. Then loop through it to retrieve each item, from which you can retrieve the video object. You probably need to use a NSMutableArray to append to your list of titles.

    NSArray *items = [data objectForKey:@"items"];

    // renamed property blogposts to titles and changed it to NSMutableArray
    self.titles = [NSMutableArray array];


    for (NSDictionary *item in items) {
        NSDictionary *video = [item objectForKey:@"video"];
        NSLog(@"%@",video);

        NSString *title = [video objectForKey:@"title"];

        NSLog(@"%@",title);

        // The titles property should be an NSMutableArray
        [self.titles addObject:title];

    }

Hello Amit,

Its still not working. Here is the error message from the console "Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x1ed78ef0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key title.'"

However after making some changes it worked, but i am not sure why its working and i dont know why i made these changes.

Here are the changes i made:

[self.titles addObject:title] was changed to [self.titles addObject:video];

cell.textLabel.text = [titles objectAtIndex:indexPath.row]; changed to cell.textLabel.text = [titles valueForKey:@"title"];