2012-07-02 4 views
1

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

+0

것 같습니다 'json'을 NSArray로 사용하고 배열 내부의 일부 객체에 액세스하려고합니다. 그러나'JSONObjectWithData : options : error :'는'id'객체를 반환 할 것이고, JSON은'objectAtIndex :'를 사용할 수없는 곳입니다. – Pfitz

+0

질문 제목을 편집하여 "해결 됨"이라고 말하지 마십시오. 허용되는 대답은 우리가 그것을 어떻게 해결할 수 있는지, 편집은 잡음에 불과하다는 것입니다. – jrturton

답변

1

에서 보여주고있다 액세스하려는 어레이가 루트 레벨에 있지 않습니다. 루트 레벨은 사전이고 찾고있는 배열은 "features"키에 있습니다. 당신의 tableView:cellForRowAtIndexPath: 방법, 그리고

NSDictionary *json; 

thusly 히 보고서에 액세스 :

먼저 NSDictionaryjson 바르 선언을 변경, 제대로 액세스하려면 당신이 선언 한 것처럼

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

고맙습니다. 작동합니다! 그러나'- (NSInteger) tableView : (UITableView *) tableView numberOfRowsInSection : (NSInteger) section' 메소드에서 json 배열 개수를 반환해야하는 곳은'json.count'입니까? –

+0

올바른 방법은 올바른 방법입니다. '- (NSInteger) tableView : (UITableView *) tableView numberOfRowsInSection : (NSInteger) 섹션 { // 섹션의 행 수를 반환합니다. NSArray * array = [json objectForKey : @ "features"]; return array.count; }' –