2012-11-19 48 views
0

我有一些問題,嘗試使用SBJson JSON來分析,我做了一些研究,我無法找到的東西有幫助...SBJson Execptions解析後(__NSArrayM objectForKey :)

我跟着如何一些博客做到這一點,但我仍然得到這個錯誤:「__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 

並通過objectForKey

[book valueForKey:@"title"]; 

使用valueForKey這一翻譯我得到這個:

(
    "Yeah" 
) 

而不是僅僅呀

而且我不想再解析(「是啊」 )至少得到是啊...

回答

5

你得到的錯誤,因爲「書」是一個數組,而不是一本字典。如果所有結果都看起來像這樣,那麼「書」只有一個外部對象。你可以只改變線定義書:

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

非常感謝!它像一個魅力:) – BaNz