2012-07-02 3 views
1

J'essayais d'analyser un JSON string from USGS. Cependant, je reçois erreur "-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x68a34c0" Fondamentalement, je voudrais analyser le USSON JSON dans la tableview. Quelqu'un peut-il aider? Voici mes codes.iOS 5 JSON à l'erreur 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; 



} 

L'erreur est montrant à la ligne earthquakeReport = [json objectAtIndex:indexPath.row];

+0

On dirait que vous avez déclaré 'json' en tant que' NSArray' et en essayant d'accéder à certains objets dans le tableau. Mais 'JSONObjectWithData: options: error:' retournera un objet 'id' et il semblerait que JSON soit un ditctionary où vous ne pouvez pas utiliser' objectAtIndex: '. – Pfitz

+0

Ne pas éditer les titres des questions pour dire "résolu". La réponse acceptée est de savoir comment nous savons qu'il est résolu, l'édition est juste du bruit. – jrturton

Répondre

1

Si vous regardez vos données JSON dans un navigateur Web (http://earthquake.usgs.gov/earthquakes/feed/geojson/all/hour) vous devriez remarquer que le tableau auquel vous essayez d'accéder n'est pas au niveau racine. Le niveau racine est un dictionnaire, et le tableau que vous recherchez est dans la clé "features".

Pour y accéder correctement, d'abord changer votre json déclaration Ivar dans un NSDictionary:

NSDictionary *json; 

Ensuite, dans votre méthode tableView:cellForRowAtIndexPath:, accéder au rapport ainsi:

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

Merci beaucoup, ça marche! Cependant, dans la table '- (NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: (NSInteger) section où j'ai besoin de retourner le nombre de tableaux json, est-ce' json.count'? –

+0

je viens compris, la bonne méthode est '- (NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: section (NSInteger) {// Retourne le nombre de lignes dans la section. NSArray * array = [json objectForKey: @ "features"]; return array.count; } ' –