我面临json和objective c的一些问题。目前我正在使用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
你处理错了。它不是一个数组,它是一个字典,键@" jsoneventtosresult "的值是数组。在JSON objectwithstring行中,将其设为nsdictionary然后指向键
或删除{" jsoneventtosresult ": and final},使其已经是一个数组
哦,我认为你必须Unicode转义你的重音字符和程度符号(测试你的JSON在jsonlint.org确保它是有效的)