json解析崩溃

时间:2014-04-18 11:05:45

标签: ios objective-c json

我无法解析这个json:

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

我正在尝试:

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

但是这条消息让我崩溃了:

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

我无法理解错误。 对象forkey似乎存在问题,但我不知道为什么。  你能帮我解决一下吗?

3 个答案:

答案 0 :(得分:1)

您正在解析数组而不是字典:

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

答案 1 :(得分:1)

在JSON结构中,

当你有

{表示字典

[表示数组

基于此,“dati”的对象是Array。

Array没有像objectForKey这样的方法:这就是你崩溃的原因。

使用字典表示&跟进rckoenes的回答

答案 2 :(得分:0)

问题是以下两行,

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

关键" dati"包含一个数组,并将该数组添加为firstZero可变数组的第一个对象。这意味着您的第一个对象不是NSDictionary而是另一个Array。

所以[firstZero objectAtIndex:0];不会返回字典。这就是为什么你不能调用[datiDict objectForKey:@"id"];的原因,因为datiDict是一个数组,而不是字典。

解决方案:只需将以上两行更改为以下内容,

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