iOS json解析器错误

标签 ios objective-c json sigabrt

我正在尝试使用 json 从数据库获取数据。在我使用 json 解析器获取数据之前,该程序可以运行,但是当我使用解析器时,该程序崩溃了。有什么问题吗? json 和 peopleList 都是 NSMutableArray。您可以在此处找到 json 的缩短版本:

{"persons":[{"id":3,"name":"Micke","userId":2,"expectations":"None","skills":"Many","description":"Yes I want to want","experience":"From everywhere","created_at":"2013-09-27T07:48:55.612Z","updated_at":"2013-09-27T07:48:55.612Z"}]}

代码如下:

{
NSURL *url = [NSURL URLWithString:getDataURL];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
peopleList = [[NSMutableArray alloc]init];


for (int i =0; i<json.count; i++) {
    NSString * pId = [[json objectAtIndex:i]objectForKey:@"id"];
    NSString * pName = [[json objectAtIndex:i]objectForKey:@"name"];
    NSString * pUserId = [[json objectAtIndex:i]objectForKey:@"userId"];
    NSString * pExpectations = [[json objectAtIndex:i]objectForKey:@"expectations"];
    NSString * pSkills = [[json objectAtIndex:i]objectForKey:@"skills"];
    NSString * pDescription = [[json objectAtIndex:i]objectForKey:@"description"];
    NSString * pExperience = [[json objectAtIndex:i]objectForKey:@"experience"];
    NSString * pCreatedAt = [[json objectAtIndex:i]objectForKey:@"created_at"];
    NSString * pUpdatedAt = [[json objectAtIndex:i]objectForKey:@"updated_at"];

    Person *person = [[Person alloc]
                       initWithID:pId andName:pName andUserId:pUserId andExpectations:pExpectations andSkills:pSkills andDescription:pDescription andExperience:pExperience andCreated_at:pCreatedAt andUpdated_at:pUpdatedAt];
    [peopleList addObject:person];
}

}

错误开始于:

2013-11-13 19:26:51.644 FENA[1664:70b] -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x109088fc0
2013-11-13 19:26:51.651 FENA[1664:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x109088fc0'

最佳答案

你有字典的字典所以试试这个

NSString * pId = [[[json objectForKey:@"persons"] objectAtIndex:i] objectForKey:@"id"];
...

关于iOS json解析器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19952899/

相关文章:

java - 使用 ObjectMapper 更改字段大小写

iphone - 如何在 iOS 上固定证书的公钥

ios - iOS 上的 Codename One 网络问题

ios - Firebase:由 FIRServerValue.timestamp() 创建的下载时间戳与在线版本略有不同

iphone - 将 double 值舍入到小数点后 2 位

ios - 访问未配置 : Google URL Shorten API on iOS Simulator

ios - XCode 上传数据文件到 iPad "Permission Denied"?

ios - 面向对象 - 父类(super class)中的 iOS isEqual 方法,它对子类是否正常工作?

mysql - 在 MySQL 中字符串化一个 JSON 数组

json - 使用 JSON-B 1.0(例如 Yasson、Java EE 8)有效地将 JsonObject 转换为 pojo