2011-11-25 28 views
0

我正在面臨與json和目標c的一些問題。 Atm我使用sbJson框架(我可以改變框架,如果有人告訴我這樣做!),我不能解析一個JSON數組。iphone與sbjson框架無法解析json數組

這就是我想要解析JSON,

{"JsonEventosResult": 
    [ 
     {"nombre":"Venta de Reposición N°13","id":34,"fecha":"16/09/2011"}, 
     {"nombre":"evento rose","id":37,"fecha":"04/10/2011"}, 
     {"nombre":"Prueba PhoneGap","id":40,"fecha":"23/11/2011"} 
    ] 
} 

這是我的iphone代碼:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {  
[connection release]; 

NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
[responseData release]; 

NSError *error; 
SBJSON *json = [[SBJSON new] autorelease]; 
NSArray *luckyNumbers = [json objectWithString:responseString error:&error]; 
[responseString release]; 

if (luckyNumbers == nil) 
    label.text = [NSString stringWithFormat:@"JSON parsing failed: %@", [error localizedDescription]]; 
else {  
    NSMutableString *text = [NSMutableString stringWithString:@"Lucky numbers:\n"]; 

    for (int i = 0; i < [luckyNumbers count]; i++) 
     [text appendFormat:@"%@\n", [luckyNumbers objectAtIndex:i]]; 

    label.text = text; 
} 
} 

我得到的錯誤是luckyNumbers是0對象的數組。

我從http://mobileorchard.com/tutorial-json-over-http-on-the-iphone/得到的樣本。

那麼問題在哪裏? JSON我得到表單服務或框架?

thx

回答

4

你正在處理它錯了。它不是一個數組,它是一個字典,key @「JsonEventosResult」的值就是數組。所以,在你的JSON objectwithstring線,做出一個NSDictionary,然後指向關鍵

或刪除{「JsonEventosResult」:和最終}所以它已經是一個數組

哦,我想你」將不得不Unicode轉義你的重音字符和度符號(在jsonlint.org測試你的JSON,以確保它是有效的)