0

I am trying to parse this json:

{
    "myData": [
        {
            "date": "2013-07-29",
            "preferredMeetingLocation": "home",
            "isbn": null,
            "category": "Clothing",
            "price": "5",
            "title": "clothingstuff",
            "description": "Desc"
        },
        {
            "date": "2013-07-29",
            "preferredMeetingLocation": "home2",
            "isbn": null,
            "category": "Clothing",
            "price": "2",
            "title": "other",
            "description": "Desc2"
        }
    ]
}

So far I have:

    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil];
    NSDictionary *results = [json objectForKey:@"myData"];
for (NSDictionary *item in results) {
    NSLog(@"results::%@", [results objectForKey:@"title"]);
}

but I get Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x8877e40'

The main goal is to be able to parse the data received and then display each set of info in a cell.

What am I doing wrong?

4

2 回答 2

2

线

 NSLog(@"results::%@", [results objectForKey:@"title"]);
 //                        ^---- Wrong variable used here!

应该

 NSLog(@"results::%@", [item objectForKey:@"title"]);
于 2013-07-29T18:17:36.103 回答
1

results应该是一个数组。而且您正在记录错误的对象。

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil];
NSArray *results = [json objectForKey:@"myData"];
for (NSDictionary *item in results) {
    NSLog(@"title::%@", [item objectForKey:@"title"]);
}
于 2013-07-29T18:19:49.500 回答