我在JSONkit.h
使用NSDictionary
时出现问题。使用它的正确方法是什么?
JSON:
[{"id":"1100","name":"John Stuart"}]
代码:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSDictionary *jsonData = [responseData objectFromJSONData];
NSString *name = [jsonData objectForKey:@"name"];
NSLog(@"Name: %@", name);
}
错误:
** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[JKArray objectForKey:]: unrecognized selector sent to instance 0x84b9b30'
答案 0 :(得分:1)
您的JSON是一个数组,但您的代码假定它是一个字典并尝试在其上调用-objectForKey:
。您可能想尝试以下操作:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSArray *jsonData = [responseData objectFromJSONData];
for (NSDictionary *dict in jsonData) {
NSString *name = [dict objectForKey:@"name"];
NSLog(@"Name: %@", name);
}
}