Your code seems fine except the result is an NSArray,
not an NSDictionary,
here is an example:
The first two lines just creates a data object with the JSON, the same as you would get reading it from the net.
NSString *jsonString = @"[{\"id\": \"1\", \"name\":\"Aaa\"}, {\"id\": \"2\", \"name\":\"Bbb\"}]";
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *e;
NSMutableArray *jsonList = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&e];
NSLog(@"jsonList: %@", jsonList);
NSLog contents (a list od dictionaries):
jsonList: (
{
id = 1;
name = Aaa;
},
{
id = 2;
name = Bbb;
}
)
本文探讨了在处理JSON数据时遇到的问题,即从NSArray解析为NSDictionary时的常见错误。通过提供实例和解决方案,帮助开发者正确理解并解决这一问题。
1万+

被折叠的 条评论
为什么被折叠?



