【问题标题】:JSON iOS failed ErrorJSON iOS 失败错误
【发布时间】:2012-02-23 04:01:35
【问题描述】:

所以我有一个返回这个 JSON 的网络服务:

[{"other_user":"54","distance":"1 ft","duration":"1 min"},{"other_user":"55","distance":"2 ft","duration":"5 min"}]

然后在我的 ios 应用中使用:

NSString *responseString = [request responseString];
    NSArray *responseArray = [responseString JSONValue];
    for (NSDictionary* item in responseArray) {
        NSString *otherUser = [item objectForKey:@"other_user"];
        NSString *otherDistance = [item objectForKey:@"distance"];
        NSString *otherDuration = [item objectForKey:@"duration"];

       NSLog(@"user: %@  distance: %@  time: %@", otherUser, otherDistance, otherDuration);
    }

但我收到此错误:

-JSONValue failed. Error is: Illegal start of token [S]

感谢任何帮助,我不知道在哪里查看/调试

【问题讨论】:

  • 验证您的 JSON 响应 (jsonlint.com) 以查看它是否是有效的 JSON 对象。您使用哪个 JSON Parser 来解析项目中的 JSON 响应?
  • 我正在使用 SBJSON,它是有效的
  • 打印出responseString。我猜这不是有效的 JSON。
  • 你说得对,我有一些回声。我现在觉得很傻。很抱歉浪费了您的时间。

标签: php ios json


【解决方案1】:

尝试以下方法:

SBJSON *parser = [[SBJSON alloc] init];
NSString *responseString = [request responseString];

NSError *jsonError=nil;
NSArray *responseArray = [parser objectWithString: responseString error: &jsonError];

if (jsonError==nil) {
   for (NSDictionary* item in responseArray) {
      NSString *otherUser = [item objectForKey:@"other_user"];
      NSString *otherDistance = [item objectForKey:@"distance"];
      NSString *otherDuration = [item objectForKey:@"duration"];

      NSLog(@"user: %@  distance: %@  time: %@", otherUser, otherDistance, otherDuration);
   }
}

祝你好运

t

【讨论】:

  • 试过这个但显然 jsonError!=nil
  • 那么你的json可能是无效的。
  • 是的,我想我在 PHP 中有一些回声,我在测试后没有注释掉。搞砸了,它现在正在工作。感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-28
  • 1970-01-01
  • 2012-04-01
  • 2018-01-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多