gpt4 book ai didi

ios - json解析崩溃

转载 作者:行者123 更新时间:2023-11-28 22:09:57 25 4
gpt4 key购买 nike

我无法解析这个 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];
}];

关于ios - json解析崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23152833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com