Enzoses
Enzoses

Reputation: 115

Crash on json parse

I'm unable to parse this json:

{
"dati": [
    {
        "id": "1",
        "nome": "Perugia01"
    },
    {
        "id": "2",
        "nome": "Perugia02"
    }
]
}

I'm trying with:

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

[manager GET:path parameters:nil success:^(NSURLSessionDataTask *task, id responseObject)
 {
     NSLog(@"Success: %@", responseObject);

     NSDictionary *jsonDict = (NSDictionary *) responseObject;

     NSMutableArray *firstZero = [[NSMutableArray alloc] init];
     [firstZero addObject:[jsonDict objectForKey:@"dati"]];

     NSMutableDictionary *weatherDict = [firstZero objectAtIndex:0];

     NSString *codice = [weatherDict objectForKey:@"id"];  //Here I got a crash
     NSString *nome = [weatherDict objectForKey:@"nome"];

     NSLog(@"Codice cantiere: %@\nNome cantiere: %@",codice, nome);
 }failure:^(NSURLSessionDataTask *task, NSError *error)
 {
     // Failure
     NSLog(@"Failure: %@", error);
     NSString *errore = [NSString stringWithFormat:@"%@",error];
     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Errore" message:errore delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
     [alert show];
 }];

but I got a crash with this message:

[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0xa1a6ee0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0xa1a6ee0'

I can't understand what's wrong. It seems there's a problem with the objectforkey, but I don't know why. Could you please help me to figure it out?

Upvotes: 0

Views: 228

Answers (3)

In JSON Structure,

when you got

{ means Dictionary

[ means Array

So based on that, object for "dati" is Array.

Array doesn't have the method like objectForKey: That's why you are getting crash.

Use dictionary for that & follow up the rckoenes answer

Upvotes: 1

Rukshan
Rukshan

Reputation: 8066

Issue is with following two lines,

 NSMutableArray *firstZero = [[NSMutableArray alloc] init];
 [firstZero addObject:[jsonDict objectForKey:@"dati"]];

The key "dati" contains an array, and you are adding that array as the first object of the firstZero mutable array. This means your first object is not an NSDictionary but another Array.

So [firstZero objectAtIndex:0]; will not return a dictionary . That's why you can not call [datiDict objectForKey:@"id"]; because datiDict is an array, not a dictionary.

Solution: Simply change above two lines to following,

 NSMutableArray *firstZero = [[NSMutableArray alloc] init];
 firstZero = [jsonDict objectForKey:@"dati"]];

Upvotes: 0

rckoenes
rckoenes

Reputation: 69469

You are parsing an array not a dictionary:

[manager GET:path parameters:nil success:^(NSURLSessionDataTask *task, id responseObject)
 {
     NSLog(@"Success: %@", responseObject);

     NSArray *dati  = [responseObject objcForKey:@"dati"]];

     for (NSDictionary *datiDict in dati) {
         NSString *codice = [datiDict objectForKey:@"id"];  //Here I got a crash
         NSString *nome = [datiDict objectForKey:@"nome"];

         NSLog(@"Codice cantiere: %@\nNome cantiere: %@",codice, nome);
     }

 }failure:^(NSURLSessionDataTask *task, NSError *error)
 {
     // Failure
     NSLog(@"Failure: %@", error);
     NSString *errore = [NSString stringWithFormat:@"%@",error];
     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Errore" message:errore delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
     [alert show];
 }];

Upvotes: 1

Related Questions