문제

I am trying to get the data from database using json. The program works before I get the data with the json parser, but crashes when I use the parser. What can be wrong? The json and peopleList are both NSMutableArray. You can find a shortened version of the json here:

{"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"}]}

And the code here:

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

}

The error starts out with:

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'
도움이 되었습니까?

해결책

You have dictionary of dictionary so try this

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

다른 팁

Your json is an NSDictionary. So you cant use objectAtIndex:. You have to use like this:

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"];
[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

returns the Dictionary so you must use this :

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

}

Because JSON Object contains persons Dictionary.

So you need to change code to:

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

}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top