本文介绍了解析后的SBJson Execptions(__ NSArrayM objectForKey :)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用SBJson解析Json时遇到了一些问题,我做了一些研究而且找不到有用的东西...

I've some problems trying to parse Json using SBJson, I did some research and I can't find something helpfull...

我关注了一些博客怎么做,但我仍然得到那个错误:_ _ NSArrayM objectForKey:

I followed some blog about how to do it, but I still get that error : "__NSArrayM objectForKey:"

所以这是我试图解析的Json:

So this is the Json I'm trying to parse :

{
"result": [
    {
        "authors": [
            "Eric Ries"
        ],
        "bc": 9780671607,
        "title": "Yeah",
        "urlImage": "www.yeah.hey",
        "description": "Hey..."
    }
    ]
}

这是我正在使用的代码:

This is the code I'm using :

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

并且通过使用objectForKey的valueForKey intead

And by using valueForKey intead of objectForKey

[book valueForKey:@"title"];

我收到了这个:

(
    "Yeah"
)

而不仅仅是啊

而且我不想再次解析(是的)至少得到呀...

And I don't want to parse again the ( "Yeah" ) to at least get Yeah...

推荐答案

您收到该错误,因为book是一个数组,而不是字典。如果所有结果看起来都像这样,那么book中只有一个外部对象。您只需将行定义书更改为:

You get that error because "book" is an array, not a dictionary. If all the results look like this one, that is "book" only has one outer object in it. You can just change the line defining book to :

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

这篇关于解析后的SBJson Execptions(__ NSArrayM objectForKey :)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 18:57