0

我面临一些关于json和objective c的问题。Atm 我正在使用 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?

谢谢

4

1 回答 1

4

你处理错了。它不是一个数组,它是一个字典,键 @"JsonEventosResult" 的值是数组。因此,在您的 JSON objectwithstring 行中,将其设为 nsdictionary,然后指向该键

或删除 {"JsonEventosResult": and final } 使其已经是一个数组

哦,我认为你必须使用 Unicode 转义你的重音字符和度数符号(在 jsonlint.org 测试你的 JSON 以确保它是有效的)

于 2011-11-25T19:04:20.300 回答