2013-02-13 51 views
-1

JSONkit.h使用NSDictionary存在問題。什麼是正確的方式來使用它?JSONKit 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

您從示例某處複製的第一行是錯誤的。最外層的JSON結構是由周圍的[]'字符表示的「數組」。 – 2013-02-13 02:32:39

回答

1

你JSON是一個數組,但你的代碼假定它是一本字典,並試圖調用-objectForKey:上它。您可能想要嘗試以下操作:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSArray *jsonData = [responseData objectFromJSONData]; 
    for (NSDictionary *dict in jsonData) { 
     NSString *name = [dict objectForKey:@"name"]; 
     NSLog(@"Name: %@", name); 
    } 
}