iOS 5 JSON到tableView错误
我试图parsing来自USGS的JSONstring 。 但是我得到错误"-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x68a34c0"
基本上,我想parsingUSGS 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];
如果您在Web浏览器中查看JSON数据( http://earthquake.usgs.gov/earthquakes/feed/geojson/all/hour ),您应该注意到您尝试访问的数组不在根目录。 根级别是一个字典,你正在寻找的数组是在“function”键。
要正确地访问它,首先将你的json
伊娃声明改成一个NSDictionary
:
NSDictionary *json;
然后,在你的tableView:cellForRowAtIndexPath:
方法中,这样访问报告:
earthquakeReport = [[json objectForKey:@"features"] objectAtIndex:indexPath.row];
- 如何显示来自Parse的图像在UIImageView(Swift)?
- 自动布局如何在一个视图中隐藏1个视图,3个等宽视图
- 每次在iPhone上启动应用程序时,都要更改启animation面
- iOS Reachability类如何工作,为什么IP地址169.254.0.0?
- 如何将AUGraph的输出写入文件?
- 未调用NSURLConnection didReceiveData
- 使用MPMoviePlayerController从iTunes中播放电影?
- 如何在swift中以编程方式更改应用程序的Localizablestring文件?
- ioscon 2017 —构建适用于iOS,macOS和tvOS的应用程序