2

我正在尝试使用 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'
4

4 回答 4

2

你有字典,所以试试这个

NSString * pId = [[[json objectForKey:@"persons"] objectAtIndex:i] objectForKey:@"id"];
...
于 2013-11-13T11:46:27.350 回答
1

json是一个NSDictionary。所以你不能使用objectAtIndex:。你必须像这样使用:

NSString * pId = [[[json objectForKey:@"persons"] objectAtIndex:i] objectForKey:@"id"];
NSString * pName = [[[json objectForKey:@"persons"] objectAtIndex:i]objectForKey:@"name"];
NSString * pUserId = [[[json objectForKey:@"persons"] objectAtIndex:i]objectForKey:@"userId"];
NSString * pExpectations = [[[json objectForKey:@"persons"] objectAtIndex:i]objectForKey:@"expectations"];
NSString * pSkills = [[[json objectForKey:@"persons"] objectAtIndex:i]objectForKey:@"skills"];
NSString * pDescription = [[[json objectForKey:@"persons"] objectAtIndex:i]objectForKey:@"description"];
NSString * pExperience = [[[json objectForKey:@"persons"] objectAtIndex:i]objectForKey:@"experience"];
NSString * pCreatedAt = [[[json objectForKey:@"persons"] objectAtIndex:i]objectForKey:@"created_at"];
NSString * pUpdatedAt = [[[json objectForKey:@"persons"] objectAtIndex:i]objectForKey:@"updated_at"];
于 2013-11-13T11:49:33.757 回答
1
[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

返回 Dictionary 所以你必须使用这个:

NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];


    for (int i =0; i<json.count; i++) {

    NSString * pId = [[[json objectForKey:@"persons"] objectAtIndex:i] objectForKey:@"id"];
    NSString * pName = [[[json objectForKey:@"persons"] objectAtIndex:i]objectForKey:@"name"];
    NSString * pUserId = [[[json objectForKey:@"persons"] objectAtIndex:i]objectForKey:@"userId"];
    NSString * pExpectations = [[[json objectForKey:@"persons"] objectAtIndex:i]objectForKey:@"expectations"];
    NSString * pSkills = [[[json objectForKey:@"persons"] objectAtIndex:i]objectForKey:@"skills"];
    NSString * pDescription = [[[json objectForKey:@"persons"] objectAtIndex:i]objectForKey:@"description"];
    NSString * pExperience = [[[json objectForKey:@"persons"] objectAtIndex:i]objectForKey:@"experience"];
    NSString * pCreatedAt = [[[json objectForKey:@"persons"] objectAtIndex:i]objectForKey:@"created_at"];
    NSString * pUpdatedAt = [[[json objectForKey:@"persons"] objectAtIndex:i]objectForKey:@"updated_at"];     

}
于 2013-11-13T11:56:43.743 回答
0

因为 JSON 对象包含人员字典。

因此,您需要将代码更改为:

{
    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 objectForKey:@"persons"] objectAtIndex:i]objectForKey:@"id"];
        NSString * pName = [[[json objectForKey:@"persons"] objectAtIndex:i]objectForKey:@"name"];
        NSString * pUserId = [[[json objectForKey:@"persons"] objectAtIndex:i]objectForKey:@"userId"];
        NSString * pExpectations = [[[json objectForKey:@"persons"] objectAtIndex:i]objectForKey:@"expectations"];
        NSString * pSkills = [[[json objectForKey:@"persons"] objectAtIndex:i]objectForKey:@"skills"];
        NSString * pDescription = [[[json objectForKey:@"persons"] objectAtIndex:i]objectForKey:@"description"];
        NSString * pExperience = [[[json objectForKey:@"persons"] objectAtIndex:i]objectForKey:@"experience"];
        NSString * pCreatedAt = [[[json objectForKey:@"persons"] objectAtIndex:i]objectForKey:@"created_at"];
        NSString * pUpdatedAt = [[[json objectForKey:@"persons"] 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-13T11:50:54.050 回答