SBJSon 不喜欢 Json 字符串中的 [ ] 字符
我注意到 SBJson 无法解码这个 json 字符串:
[{"JNAME":"VERSION","DATE": "20111012","TIME": "145020"}]
如果我删除 [ ],该字符串将成功解码并返回 NSDictionnary
我的代码:
不工作(返回 nil):
SBJsonParser *parser = [[SBJsonParser new] autorelease];
NSError *error = nil;
NSDictionary *dict = [ parser objectWithString:result error:&error ];
工作:
SBJsonParser *parser = [[SBJsonParser new] autorelease];
NSInteger len = [result length];
NSError *error = nil
NSString *result2 = [result substringWithRange:NSMakeRange(1, len - 2 ) ];
NSDictionary *dict = [ parser objectWithString:result error:&error ];
有什么想法吗?这很奇怪,因为如果我使用在线解析器或其他 Json 解码函数(例如:使用 PHP),字符串就会成功解码:
php > $json = '[{"JNAME":"VERSION","DATE": "20111012","TIME": "145020"}]';
php > print_r(json_decode($json));
Array
(
[0] => stdClass Object
(
[JNAME] => VERSION
[DATE] => 20111012
[TIME] => 145020
)
)
I noticed SBJson failed to decode this json string:
[{"JNAME":"VERSION","DATE": "20111012","TIME": "145020"}]
If I remove the [ ] the string is successuffly decoded and return a NSDictionnary
my code:
not working (return nil):
SBJsonParser *parser = [[SBJsonParser new] autorelease];
NSError *error = nil;
NSDictionary *dict = [ parser objectWithString:result error:&error ];
working:
SBJsonParser *parser = [[SBJsonParser new] autorelease];
NSInteger len = [result length];
NSError *error = nil
NSString *result2 = [result substringWithRange:NSMakeRange(1, len - 2 ) ];
NSDictionary *dict = [ parser objectWithString:result error:&error ];
Any ideas why? it s strange because if I use online parser or others Json decode functions (ex : with PHP) the string is successfully decoded :
php > $json = '[{"JNAME":"VERSION","DATE": "20111012","TIME": "145020"}]';
php > print_r(json_decode($json));
Array
(
[0] => stdClass Object
(
[JNAME] => VERSION
[DATE] => 20111012
[TIME] => 145020
)
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你想要一个数组,你应该让
NSArray 有 1 个 NSDictionary 元素。您可以在此处检查 JSON 有效性。请注意,解析器将为您提供一个字典,其中包含键@“arrayname”的数组。
If you want an array you should have
the NSArray will have 1 NSDictionary element. You can check JSON validity here. Note the parser will give you a dictionary with the array for key @"arrayname".