Pergunta

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

Foi útil?

Solução

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)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top