JSON string from USGS을 구문 분석하려고했습니다. 그러나 오류가 "-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x68a34c0"
기본적으로, 나는 tableview로 USGS JSON을 구문 분석하고 싶습니다. 누구든지 도와 줄 수 있습니까? 여기 내 코드가 있습니다. 당신은 당신이 통지해야한다 (http://earthquake.usgs.gov/earthquakes/feed/geojson/all/hour) 웹 브라우저에서 JSON 데이터를 보면 iOS 5 JSON에서 tableView로 오류
@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;
}
ViewController.h
earthquakeReport = [json objectAtIndex:indexPath.row];
것 같습니다 'json'을 NSArray로 사용하고 배열 내부의 일부 객체에 액세스하려고합니다. 그러나'JSONObjectWithData : options : error :'는'id'객체를 반환 할 것이고, JSON은'objectAtIndex :'를 사용할 수없는 곳입니다. – Pfitz
질문 제목을 편집하여 "해결 됨"이라고 말하지 마십시오. 허용되는 대답은 우리가 그것을 어떻게 해결할 수 있는지, 편집은 잡음에 불과하다는 것입니다. – jrturton