objective-c - SBJSon 不喜欢 Json 字符串中的 [ ] 字符

标签 objective-c ios json sbjson

我注意到 SBJson 无法解码这个 json 字符串:

[{"JNAME":"VERSION","DATE": "20111012","TIME": "145020"}]

如果我删除 [ ],字符串将成功解码并返回 NSDictionnary

我的代码:

不工作(返回零):

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
     )

 )

最佳答案

如果你想要一个数组,你应该有

{"arrayname" : [{"JNAME":"VERSION","DATE": "20111012","TIME": "145020"}]}

NSArray 将有 1 个 NSDictionary 元素。您可以检查 JSON 有效性 here .请注意,解析器将为您提供一个字典,其中包含键@“arrayname”的数组。

关于objective-c - SBJSon 不喜欢 Json 字符串中的 [ ] 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7753978/

相关文章:

ios - BarButtonItem 上显示的 UIAlertController 未正确居中

objective-c - 使用未声明的标识符

iphone - initWithNibName 是否需要释放

ios - 状态栏颜色 : preferredStatusBarStyle doesn't work

jquery - 如何使用 AJAX 从 JQuery 中的对象附加值

objective-c - NSNumber 保存 double

ios - CGPath 与 UIBezierPath

iphone - 相当于iOS中的R

c# - 字典的 JSON 序列化为名称 :value instead of {name:name, 值:值}

mysql - 从使用 MySQL _FETCH 设置的字符串中删除双引号的最佳方法是什么?