我正在尝试解析这个 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"
}
]
}
到目前为止我有:
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil];
NSDictionary *results = [json objectForKey:@"myData"];
for (NSDictionary *item in results) {
NSLog(@"results::%@", [results objectForKey:@"title"]);
}
但我得到 Termating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x8877e40'
主要目标是能够解析接收到的数据,然后在单元格中显示每组信息。
我做错了什么?
最佳答案
线
NSLog(@"results::%@", [results objectForKey:@"title"]);
// ^---- Wrong variable used here!
应该是
NSLog(@"results::%@", [item objectForKey:@"title"]);
关于ios - 使用 NSJSONSerialization 解析 json 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17931243/