iphone with sbjson framework cant parse json array

喜夏-厌秋 提交于 2019-12-12 01:56:38

问题


Im facing some problem with json and objective c. Atm i am using sbJson framework (i can change framework if some tell me do it!) and im not being able to parse a json array.

this is the json i want to parse,

{"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"}
    ]
}

this is my code on 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;
}
}

the error i get is that luckyNumbers is an array with 0 object.

the sample i got if from http://mobileorchard.com/tutorial-json-over-http-on-the-iphone/ .

so where is the problem? the json i get form service or the framework ?

thx


回答1:


You're handling it wrong. It's not an array, it's a dictionary, the value for key @"JsonEventosResult" is the array. So In your JSON objectwithstring line, make that an nsdictionary and then point to that key

OR remove the {"JsonEventosResult": and final } so that it already is an array

Oh, and I think you'll have to Unicode escape your accented characters and degree symbol (test your JSON at jsonlint.org to make sure it's valid)



来源:https://stackoverflow.com/questions/8273171/iphone-with-sbjson-framework-cant-parse-json-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!