iphone - SBJson Execptions after parsing (__NSArrayM objectForKey :)

标签 iphone ios json parsing sbjson

我在尝试使用 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 ];

// Get result in a NSDictionary
jsonResults = (NSDictionary*) [ json objectWithString:output error:&jsonError ];

// Check if there is an error
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") 至少得到 Yeah...

最佳答案

你得到那个错误是因为“书”是一个数组,而不是字典。如果所有的结果都是这个样子,那就是“书”里面只有一个外部对象。您只需将定义书的行更改为:

NSDictionary *book = [[ jsonResults objectForKey:@"result"] lastObject];

关于iphone - SBJson Execptions after parsing (__NSArrayM objectForKey :),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13460824/

相关文章:

iphone - 有关iOS上的本地通知和推送通知的问题

iphone - 如何自定义 iOS 警报 View ?

iphone - 获取 CGImageSourceRef 的图像路径

python - 从 python 中的字典列表创建分层 json 转储

javascript - 获取对象中的 Base64 作为字符串

iphone - 模态视图 Controller 无法在横向模式下启动

ios - 如何使用 XCode 5 在 iOS7 上调试 cordova 应用程序

ios - 使用 Swift 在 iOS 上无限滚动

ios - 是否可以创建从右到左过渡的自定义 Segue

json - 如何在 React 中渲染 JSON