2012-11-19 2 views
0

나는 몇 가지 문제가 JSON은 SBJson를 사용하여 분석을 시도했습니다, 나는 몇 가지 조사를했고, 내가 뭔가 helpfull을 찾을 수 없습니다 ... SBJson Execptions 분석 후 (__NSArrayM objectForKey :

내가하는 방법에 대한 몇 가지 블로그를 따라 그것을 할,하지만 난 여전히 오류 얻을 : "__NSArrayM objectForKey :"

: 이것은 내가 사용하고 코드가

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

그래서 이것은 JSON 내가 구문 분석을 시도하고있다

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의 intead에 의해

내가이납니다 :

(
    "Yeah" 
) 

대신에 단지 네

그리고 다시 구문 분석하고 싶지 않습니다. ("예") 적어도 그래 그래 ...

답변

5

"book"은 사전이 아니기 때문에 오류가 발생합니다. 결과가 모두 이와 같이 보이면 "책"에는 하나의 외부 객체 만 있습니다. 책을 정의하는 행을 다음과 같이 바꿀 수 있습니다.

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

감사합니다. 그것은 매력처럼 작동합니다 :) – BaNz