JSON 解析错误

发布于 2024-11-28 03:12:15 字数 615 浏览 2 评论 0原文

我在 iOS 上使用 SBJson 框架(也称为 json-framework)。

解析某个 JSON 文件时,出现以下错误: -JSONValue 失败。错误是:未转义的控制字符 [0x09]'

我已经多次使用这个框架,并且还在同一个应用程序中解析一个非常相似的 JSON 文件(甚至更长),并且它工作正常。

我尝试扔掉一堆 NSLogs,一切似乎都很好。有人可以告诉我这个错误意味着什么,或者至少如何继续调试这样的错误吗?

以下是显示错误的代码:

- (void)downloadSchedule:(NSString *)jsonString {

    // Get JSON feed URL and instantiate a dictionary object with its content
    NSDictionary *topDic = [jsonString JSONValue];

    NSLog(@"topDic count %d", [topDic count]);

topDic 显示计数为 0。错误位于 [jsonString JSONValue] 行。

谢谢

I am using SBJson framework (also known as json-framework) for the iOS.

When parsing a certain JSON file, I am getting the following error:
-JSONValue failed. Error is: Unescaped control character [0x09]'

I have used this framework many times and I am also parsing a very similar JSON file (that is even much longer) in that same app and it's working fine.

I tried throwing around a bunch of NSLogs and everything seems to be fine. Can someone please point me to what this error means, or at least how to go ahead in debugging such an error?

Here is the code that displays the error:

- (void)downloadSchedule:(NSString *)jsonString {

    // Get JSON feed URL and instantiate a dictionary object with its content
    NSDictionary *topDic = [jsonString JSONValue];

    NSLog(@"topDic count %d", [topDic count]);

topDic is showing a count of 0. The error is at the [jsonString JSONValue] line.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

神经大条 2024-12-05 03:12:15

我有一个很好的解决方案。应用此方法删除转义字符。

-(NSString *)removeUnescapedCharacter:(NSString *)inputStr
{

NSCharacterSet *controlChars = [NSCharacterSet controlCharacterSet];

NSRange range = [inputStr rangeOfCharacterFromSet:controlChars];

  if (range.location != NSNotFound) 
  {

      NSMutableString *mutable = [NSMutableString stringWithString:inputStr];

      while (range.location != NSNotFound) 
      {

          [mutable deleteCharactersInRange:range];

          range = [mutable rangeOfCharacterFromSet:controlChars];

      }

      return mutable;

   }

  return inputStr;
}

通过像这样传递输出字符串来调用此方法

NSString *output = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"yourUrlString"] encoding:NSUTF8StringEncoding error:nil];

output = [self removeUnescapedCharacter:output];

I have a great solution for it. Apply this method for remove escaped characters.

-(NSString *)removeUnescapedCharacter:(NSString *)inputStr
{

NSCharacterSet *controlChars = [NSCharacterSet controlCharacterSet];

NSRange range = [inputStr rangeOfCharacterFromSet:controlChars];

  if (range.location != NSNotFound) 
  {

      NSMutableString *mutable = [NSMutableString stringWithString:inputStr];

      while (range.location != NSNotFound) 
      {

          [mutable deleteCharactersInRange:range];

          range = [mutable rangeOfCharacterFromSet:controlChars];

      }

      return mutable;

   }

  return inputStr;
}

Call this method with passing your output string like this

NSString *output = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"yourUrlString"] encoding:NSUTF8StringEncoding error:nil];

output = [self removeUnescapedCharacter:output];
爱的那么颓废 2024-12-05 03:12:15

我猜您的文件包含一个未编码的选项卡 (ascii 0x09),根据 json 语法。

I guess your file contain an unencoded tab (ascii 0x09) that should be replaced with \t according to the json grammar.

围归者 2024-12-05 03:12:15

看一下 http://www.json.org/ 有一些字符需要转义可以被 JSON 正确解析。这就是原因。该文件不是正确的 JSON。

Have a look at http://www.json.org/ There are some characters that need to be escaped to be properly parsed by JSON. This is the cause. The file is not proper JSON.

我ぃ本無心為│何有愛 2024-12-05 03:12:15

如果你的文件有 '\n' 或 '\r' 类型的 html qoutes 那么它可能会导致 obj-c 错误。
您可以添加:

[jsonString stringByReplacingOccurrencesOfString:@"\r\n" withString:@"
"]

我遇到了同样的问题并使用此解决了。

If your file is having '\n' or '\r' kind of html qoutes then it may cause error in obj-c.
You can add :

[jsonString stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br />"]

I was having same problem and solved using this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文