【问题标题】:Invalid characters in JSON outputJSON 输出中的无效字符
【发布时间】:2012-06-07 20:12:03
【问题描述】:

我从我的服务器获取了一些代表搜索结果的 JSON。我从 Sphider 获得它,它是一个 PHP 搜索引擎。如果您有兴趣,我已经询问过如何获取 JSON here

所以这个 JSON 对我来说看起来不错。当我将它放入this validator 时,它说“发现无效字符。[代码 18,结构 66]”我不确定这是什么意思,但我想我应该更仔细地看看它抱怨的那一行看看有没有什么奇怪的字符。我发现的只是 0x20s(空格)应该在哪里。

现在,我认为最大的提示是:当我将验证器输出中的“无效”JSON 复制到输入中并再次运行它时,它会起作用,说明 JSON 是有效的。我不知道该怎么做。

那么验证器是什么意思呢?另外,这个文本是从数据库中提取的,是什么导致它输出“无效字符”?

这是 JSON:

{   "result_report":"Displaying results 1 - 2 of 2 matches (0.03 seconds) ",  "results":[    {   "idented":"false",   "num":"1",  "weight":"[100.00%]",  "link":"http://www.avtainsys.com/articles/PHP_Markdown",  "title":"PHP <b>Markdown</b>",   "description":"  Full documentation of <b>Markdown</b>'s syntax is available on John's <b>Markdown</b> page: http://daringfireball.net/projects/<b>Markdown</b>/   Installation and Requirement  PHP <b>Markdown</b> requires PHP version 4.0.5 or later.  WordPress  PHP <b>Markdown</b>",  "link2":"http://www.avtainsys.com/articles/PHP_Markdown",  "size":"31.0kb" },  {   "idented":"false",   "num":"2",  "weight":"[3.46%]",  "link":"http://www.avtainsys.com/articles",  "title":"Recent Articles",   "description":" systems         PHP <b>Markdown</b>     by Some Guy     Some information regarding using the PHP <b>Markdown</b> library, it's deployment, and some things to watch out for.            MD Example      by Hassan El-Mehalawi       A simple example",  "link2":"http://www.avtainsys.com/articles",  "size":"3.3kb" },  {  "num":"-1" }  ],  "other_pages":[   {  "title":"1",  "link":"search.php?query=markdown&start=1&search=1&results=10&type=and&domain=",      "active":"true"     }     ]  }

这里是格式化的:

{
   "result_report":"Displaying results 1 - 2 of 2 matches (0.03 seconds) ",
   "results":[
      {
         "idented":"false",
         "num":"1",
         "weight":"[100.00%]",
         "link":"http://www.avtainsys.com/articles/PHP_Markdown",
         "title":"PHP <b>Markdown</b>",
         "description":"  Full documentation of <b>Markdown</b>'s syntax is available on John's <b>Markdown</b> page: http://daringfireball.net/projects/<b>Markdown</b>/   Installation and Requirement  PHP <b>Markdown</b> requires PHP version 4.0.5 or later.  WordPress  PHP <b>Markdown</b>",
         "link2":"http://www.avtainsys.com/articles/PHP_Markdown",
         "size":"31.0kb"
      },
      {
         "idented":"false",
         "num":"2",
         "weight":"[3.46%]",
         "link":"http://www.avtainsys.com/articles",
         "title":"Recent Articles",
         "description":" systems       PHP <b>Markdown</b>    by Some Guy   Some information regarding using the PHP <b>Markdown</b> library, it's deployment, and some things to watch out for.         MD Example    by Hassan El-Mehalawi   A simple example",
         "link2":"http://www.avtainsys.com/articles",
         "size":"3.3kb"
      },
      {
         "num":"-1"
      }
   ],
   "other_pages":[
      {
         "title":"1",
         "link":"search.php?query=markdown&start=1&search=1&results=10&type=and&domain=",
         "active":"true"
      }
   ]
}

需要明确的是,格式化的 JSON 是使用我上面链接的格式化程序/验证器格式化的。这是我输入原始“无效”输出后得到的输出。

由于观察到上面未格式化的 JSON 实际上是有效的,这里是输出上述 JSON 的 URL:

http://avtainsys.com/sphider/search.php?query=markdown&search=1

【问题讨论】:

  • 这是一个疯狂的猜测,所以我不会把它作为答案,但它可能是&lt;b&gt; 标签吗?它可能不喜欢&lt;,但是当您从网络输出中复制它时,它会被转换为%3C;,JSON 可能希望更好地编辑:在验证器上测试它,JSON 不介意
  • 但我仍然可以在格式化的 JSON 中看到“”标签。我错过了吗?
  • 当我从这个站点复制并粘贴您的 JSON 到验证器时,我没有收到任何错误。如果能够重新创建错误,这将很难弄清楚。
  • @gbtimmon 哦,你是对的。我刚刚从这个页面复制了 JSON,它可以工作,但是直接来自服务器的相同 JSON 给出了错误。这有点问题。
  • 当我把它放入验证器时,它说它是有效的。 Chrome 也可以解析。

标签: database json


【解决方案1】:

它与标签有关;它在清除验证器标识的字符串中的空格后解析(来自实际源)。从技术上讲,标签应该在 JSON 字符串中转义为 \t(尽管有些解析器可能不关心)。

【讨论】:

  • 这确实是问题所在。在另一个 PHP 中,我将“\t”替换为“”(一个空格),这很有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-11
  • 2018-10-14
  • 1970-01-01
  • 1970-01-01
  • 2013-02-17
  • 1970-01-01
  • 2015-04-23
相关资源
最近更新 更多