iOS 5 JSON 到 tableView 错误 [英] iOS 5 JSON to tableView error

查看:75
本文介绍了iOS 5 JSON 到 tableView 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析来自 USGS 的 JSON 字符串.但是我收到错误 "-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x68a34c0" 基本上,我想将 USGS JSON 解析到 tableview 中.任何人都可以帮忙吗?这是我的代码.

I was trying to parse a JSON string from USGS. However I get error "-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x68a34c0" Basically, I would like to parse the USGS JSON into the tableview. Can anyone help? Here are my codes.

ViewController.h

@interface EarthquakeViewController : UITableViewController
{
    NSArray *json;
    NSDictionary *earthquakeReport;
}

ViewController.m

- (void)viewDidLoad
{
    //content = [NSMutableArray arrayWithObjects:@"one", @"two", nil];
    [super viewDidLoad];
    [self fetchReports];
}
- (void)fetchReports
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSData* data = [NSData dataWithContentsOfURL:
                        [NSURL URLWithString: @"http://earthquake.usgs.gov/earthquakes/feed/geojson/all/hour"]];

        NSError* error;
        json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
        NSLog(@"%@", json);
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    });
}
- (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 json.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    earthquakeReport = [json objectAtIndex:indexPath.row];
    NSString *country = [[[earthquakeReport objectForKey:@"features"] objectForKey:@"properties"] objectForKey:@"place"];
    NSString *mag = [[[earthquakeReport objectForKey:@"features"] objectForKey:@"properties"] objectForKey:@"mag"];
    cell.textLabel.text = country;
    cell.detailTextLabel.text = mag;

    return cell;



}

错误显示在 earthquakeReport = [json objectAtIndex:indexPath.row];

推荐答案

如果您在 Web 浏览器中查看 JSON 数据 (http://earthquake.usgs.gov/earthquakes/feed/geojson/all/hour) 您应该注意到您尝试访问的阵列不在根等级.根级别是一个字典,你要找的数组在features"键中.

If you look at your JSON data in a web browser (http://earthquake.usgs.gov/earthquakes/feed/geojson/all/hour) you should notice that the array you are trying to access is not at the root level. The root level is a dictionary, and the array you're looking for is in the "features" key.

要正确访问它,首先将您的 json ivar 声明更改为 NSDictionary:

To access it properly, first change your json ivar declaration into an NSDictionary:

NSDictionary *json;

然后,在您的 tableView:cellForRowAtIndexPath: 方法中,这样访问报告:

Then, in your tableView:cellForRowAtIndexPath: method, access the report thusly:

earthquakeReport = [[json objectForKey:@"features"] objectAtIndex:indexPath.row];

这篇关于iOS 5 JSON 到 tableView 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆