Ios 使用NSJSONSerialization解析json结果
我正在尝试解析此json:Ios 使用NSJSONSerialization解析json结果,ios,objective-c,json,Ios,Objective C,Json,我正在尝试解析此json: { "myData": [ { "date": "2013-07-29", "preferredMeetingLocation": "home", "isbn": null, "category": "Clothing", "price": "5", "title": "clothingstuff",
{
"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"]);
}
但由于未捕获的异常“NSInvalidArgumentException”,我得到了终止应用程序的,原因:'-[\u NSCFArray objectForKey:]:未识别的选择器发送到实例0x8877e40'
主要目标是能够解析接收到的数据,然后在单元格中显示每组信息
我做错了什么 线路
NSLog(@"results::%@", [results objectForKey:@"title"]);
// ^---- Wrong variable used here!
应该是
NSLog(@"results::%@", [item objectForKey:@"title"]);
线路
NSLog(@"results::%@", [results objectForKey:@"title"]);
// ^---- Wrong variable used here!
应该是
NSLog(@"results::%@", [item objectForKey:@"title"]);
结果
应该是一个数组。而您正在记录错误的对象
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil];
NSArray *results = [json objectForKey:@"myData"];
for (NSDictionary *item in results) {
NSLog(@"title::%@", [item objectForKey:@"title"]);
}
结果
应该是一个数组。而您正在记录错误的对象
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil];
NSArray *results = [json objectForKey:@"myData"];
for (NSDictionary *item in results) {
NSLog(@"title::%@", [item objectForKey:@"title"]);
}
啊,谢谢,如果我想知道归还了多少物品的数量,在这个例子中是2,我怎么得到?我认为这有点像
[[results allKeys]count]代码>但这会崩溃为well@BluGeni:注意,results
应该是一个数组:NSArray*results=…
正如@rmaddy在他的回答中正确地说的那样。然后,[results count]
应该可以了。啊,谢谢,如果我想知道退回了多少物品的数量,在本例中是2,我该如何获得?我认为这有点像[[results allKeys]count]代码>但这会崩溃为well@BluGeni:注意,results
应该是一个数组:NSArray*results=…
正如@rmaddy在他的回答中正确地说的那样。然后,[results count]
应该会起作用。您正在做的正是消息所说的:尝试在NSArray上执行“objectForKey”。您正在做的正是消息所说的:尝试在NSArray上执行“objectForKey”。