【问题标题】:iOS 5 JSON to tableView erroriOS 5 JSON 到 tableView 错误
【发布时间】:2012-07-02 11:40:25
【问题描述】:

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

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

【问题讨论】:

  • 看起来您已将 json 声明为 NSArray 并尝试访问数组中的某些对象。但是JSONObjectWithData:options:error: 将返回一个id 对象,并且您似乎JSON 是一个字典,您不能使用objectAtIndex:
  • 请勿将问题标题编辑为“已解决”。公认的答案是我们如何知道它已解决,编辑只是噪音。

标签: objective-c ios json tableview nsjsonserialization


【解决方案1】:

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

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

NSDictionary *json;

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

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

【讨论】:

  • 非常感谢,它有效!但是,在我需要返回 json 数组计数的 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 方法中,是 json.count?
  • 我刚刚想通了,正确的方法是- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. NSArray *array = [json objectForKey:@"features"]; return array.count; }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-25
  • 2016-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多