0

我在JSONkit.h使用中遇到问题NSDictionary。使用它的正确方法是什么?

杰森:

[{"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'
4

1 回答 1

2

您的 JSON 是一个数组,但您的代码假定它是一个字典并尝试调用-objectForKey:它。您可能想尝试以下方法:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSArray *jsonData = [responseData objectFromJSONData];
    for (NSDictionary *dict in jsonData) {
        NSString *name = [dict objectForKey:@"name"];
        NSLog(@"Name: %@", name);
    }
}
于 2013-02-13T00:06:54.880 回答