下面码分析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
朋友,你能告诉我如何以简单的方式发送这个请求到服务器。你给出的例子太奇怪了。 – 2012-08-05 09:32:59
我尝试了几次使用NSURLRequest和NSURLConnection等标准函数来发出HTTP请求,但这些方法对我来说是非常痛苦的。最后,我决定使用网络库“AFNetworking”,通过它可以在几行代码中获得JSON。 – weed 2012-08-05 10:12:03
对不起朋友,我是用iphone编码的文盲。请帮助我请求AFNetworking的方式。请给我代码。 – 2012-08-05 10:43:14