开发者

SBJSon dont like [ ] chars in Json string

开发者 https://www.devze.com 2023-04-12 23:03 出处:网络
I noticed SBJson failed to decode this json string: [{\"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:&开发者_如何学Python;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
     )

 )


If you want an array you should have

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

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".

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号