Ios json解析崩溃

Ios json解析崩溃,ios,objective-c,json,Ios,Objective C,Json,我无法解析此json: { "dati": [ { "id": "1", "nome": "Perugia01" }, { "id": "2", "nome": "Perugia02" } ] } 我正在尝试: AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL]; manager

我无法解析此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'
我不明白怎么了。 objectforkey似乎有问题,但我不知道为什么。
你能帮我弄清楚吗?

你在分析数组而不是字典:

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

问题在于以下两条线

 NSMutableArray *firstZero = [[NSMutableArray alloc] init];
 [firstZero addObject:[jsonDict objectForKey:@"dati"]];
键“dati”包含一个数组,您将该数组添加为
firstZero
可变数组的第一个对象。这意味着您的第一个对象不是NSDictionary,而是另一个数组

所以
[firstZero objectAtIndex:0]不会返回字典。这就是为什么不能调用
[datiDict objectForKey:@“id”]datiDict
是数组,而不是字典

解决方案:只需将上面两行更改为下面两行

 NSMutableArray *firstZero = [[NSMutableArray alloc] init];
 firstZero = [jsonDict objectForKey:@"dati"]];
在JSON结构中

你什么时候

{表示词典

[表示数组

基于此,“dati”的对象是数组

数组没有像objectForKey那样的方法:这就是为什么会出现崩溃


使用dictionary并跟进rckoenes的答案

objectForKey是一个dictionary方法,而不是数组方法。您的问题是您需要一个dictionary,但却得到了一个数组。将NSMutableDictionary*weatherDict=[firstZero objectAtIndex:0];更改为NSMutableArray*weatherArray=[firstZero objectAtIndex:0];您知道如何读取JSON吗?请访问JSON.org并学习语法。(为什么要将该值插入
firstZero
中,然后再将其拉出,而不是仅仅使用指向对象的简单指针?我怀疑您只是在修改不懂的代码。)为什么要“初始化”
firstZero
如果要在下一行中覆盖它??