iOS 5 JSON to tableView error

左心房为你撑大大i 提交于 2019-12-23 02:22:09

问题


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;



}

The error is showing at the line earthquakeReport = [json objectAtIndex:indexPath.row];


回答1:


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.

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

NSDictionary *json;

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

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


来源:https://stackoverflow.com/questions/11287219/ios-5-json-to-tableview-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!