下面的代码将 YouTube JSON 解析为表格视图。
- (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;
}
获取 JSON 的代码如下:
- (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];
}
您应该阅读 AFNetworking 入门:
https://github.com/AFNetworking/AFNetworking/wiki/Getting-Started-with-AFNetworking
您可以从 GitHub 下载示例项目并运行它:
https://github.com/weed/p120805_YouTubeJsonParse