带有sbjson框架的iPhone无法解析json数组 [英] iphone with sbjson framework cant parse json array
问题描述
我在json和目标c方面遇到了一些问题. Atm我正在使用sbJson框架(如果有人告诉我,我可以更改框架!),而且我无法解析json数组.
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.
这是我要解析的json
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"}
]
}
这是我在iPhone上的代码:
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;
}
}
我得到的错误是luckyNumbers是一个具有0个对象的数组.
the error i get is that luckyNumbers is an array with 0 object.
我从 http://mobileorchard.com获得的示例/tutorial-json-over-http-on-the-iphone/.
那么问题出在哪里呢?我得到表单服务或框架的json吗?
so where is the problem? the json i get form service or the framework ?
thx
推荐答案
您正在处理错误.它不是数组,而是字典,键@"JsonEventosResult"的值是数组.因此,在您的JSON objectwithstring行中,将其设为nsdictionary,然后指向该键
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
或删除{"JsonEventosResult":和final},使其已经是数组
OR remove the {"JsonEventosResult": and final } so that it already is an array
哦,我想您必须Unicode转义带重音符号和度数的符号(在jsonlint.org上测试JSON以确保其有效)
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)
这篇关于带有sbjson框架的iPhone无法解析json数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!