在用SBJson解析Json的时候出现问题,总是得到错误:"__NSArrayM objectForKey:"
要解析的Json:
{
"result": [
{
"authors": [
"Eric Ries"
],
"bc": 9780671607,
"title": "Yeah",
"urlImage": "www.yeah.hey",
"description": "Hey..."
}
]
}
代码:
SBJsonParser *json;
NSDictionary *jsonResults;
NSError *jsonError;
json = [ SBJsonParser new ];
// 在NSDictionary获取结果
jsonResults = (NSDictionary*) [ json objectWithString:output error:&jsonError ];
// 检测错误
if (jsonResults == nil) {
NSLog(@"Erreur lors de la lecture du code JSON (%@).", [ jsonError localizedDescription ]);
} else {
NSDictionary *book = (NSDictionary *)[ jsonResults objectForKey:@"result"];
NSArray *items = (NSArray *) [book objectForKey:@"title"];
}
错误提示:
-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x7a2d390
2012-11-19 20:32:36.336 FMS[500:11f03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x7a2d390'
*** First throw call stack:
(0x2245012 0x16a3e7e 0x22d04bd 0x2234bbc 0x223494e 0x8c6a 0x36093 0xb39e83 0x2204376 0x2203e06 0x21eba82 0x21eaf44 0x21eae1b 0x219f7e3 0x219f668 0x8365c 0x2d6d 0x2c95)
libc++abi.dylib: terminate called throwing an exception
Current language: auto; currently objective-c
用valueForKey代替objectForKey的话
[book valueForKey:@"title"];
得到的结果是:
(
"Yeah"
)
我想要的不是"yeah"结果。
你的“book”是一个数组,不是dictionary。因此book只有一个outer object,在book前加这一句:
NSDictionary *book = [[ jsonResults objectForKey:@"result"] lastObject];
刚转过来,稀里糊涂照你这把问题还解决了!
我也遇到这个问题,但我是因为NSArray 的 arrayWithObject 和 arrayWithArray 弄错了。
希望对朋友们有帮助!
我也遇到这个问题,但我是因为NSArray 的 arrayWithObject 和 arrayWithArray 弄错了。
因为这两个弄错导致本来是[{}] 的结构变成 [[{}]] , 多嵌套了一层数组,解析的时候就报了楼主那个错。
希望对朋友们有帮助!