Skip to content

Instantly share code, notes, and snippets.

@tatsuro-ueda
Created August 11, 2012 10:40
Show Gist options
  • Save tatsuro-ueda/3323706 to your computer and use it in GitHub Desktop.
Save tatsuro-ueda/3323706 to your computer and use it in GitHub Desktop.
【テーブル】【JSON】【AFNetworking】YouTube APIで得たJSONを使ってテーブルを作るには

##YouTube Video Api in JSON Format

Below code parse YouTube JSON to table view.

ss

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    if (indexPath.row < [[_JSON valueForKeyPath:@"data.items.title"] count]) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        cell.textLabel.text = 
        [[_JSON valueForKeyPath:@"data.items.title"] objectAtIndex:indexPath.row];
        cell.detailTextLabel.text =
        [[_JSON valueForKeyPath:@"data.items.description"] objectAtIndex:indexPath.row];
    }
    return cell;
}

The code to get JSON is below:

- (IBAction)refresh:(id)sender {
    NSURL *url = [NSURL URLWithString:kStrJsonURL];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id getJSON) {
        _JSON = getJSON;
        NSLog(@"%@", _JSON);
        [self.tableView reloadData];
    } failure:nil];
    [operation start];        
}

You should read getting started about AFNetworking:

https://github.com/AFNetworking/AFNetworking/wiki/Getting-Started-with-AFNetworking

You can download sample project from GitHub and just run it:

https://github.com/weed/p120805_YouTubeJsonParse

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment