【问题标题】:Invalid Json Error in JsonLintJsonLint 中的无效 Json 错误
【发布时间】:2014-02-10 10:46:08
【问题描述】:

我有一个返回以下 json 的 Web 服务:

[
    {
        "id": "9469",
        "title": "A person grabbed by police because being Nigerian he was carrying a Ghanaian passport!",
        "introtext": "A person has grabbed by police because being Nigerian he was having a Ghanaian passport! 

An individual has gotten by police on the grounds that being Nigerian he was having a Ghanaian visa! 

A 29-year-old Nigerian has been captured for endeavoring to get a visa with a falsely acquired Ghanaian travel permit."
    }
]

JSONLint 出现以下错误:

Parse error on line 5:
...       "introtext": "A person has grabbe
-----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['

我真的无法理解是什么让 json 在这里无效?是换行还是什么?我该怎么做才能让它发挥作用?谢谢。

【问题讨论】:

  • 我相信它们是你字符串中的换行符

标签: json jsonlint


【解决方案1】:

introtex 中出现新行,这是有效的 json 检查它

         [
        {
            "id": "9469",
            "title": "A person grabbed by police because being Nigerian he was carrying a Ghanaian passport!",
            "introtext": "A person has grabbed by police because being Nigerian he was having a Ghanaian passport! An individual has gotten by police on the grounds that being Nigerian he was having a Ghanaian visa! A 29-year-old Nigerian has been captured for endeavoring to get a visa with a falsely acquired Ghanaian travel permit."
        }
    ]

【讨论】:

  • 但我想在我的 json 中换行。我该怎么办?
  • 当你需要换行时使用\n
  • @anum90 — 定义“不工作”。我怀疑您是通过手动将某种编程语言中的字符串混合在一起来生成 JSON,并且 \n 被该编程语言解释为新行,而您仍在 JSON 中输出文字新行。
【解决方案2】:

将整个字符串放在一行中。 例子

[
{
    "id": "9469",
    "title": "A person grabbed by police because being Nigerian he was carrying a Ghanaian passport!",
    "introtext": "A person has grabbed by police because being Nigerian he was having aGhanaian passport!  An individual has gotten by police on the grounds that being Nigerian he was having a Ghanaian visa! A 29-year-old Nigerian has been captured for endeavoring to get a visa with a falsely acquired Ghanaian travel permit."
}

]

如果你想换行使用

"introtext": "A person has grabbed by police because \n being Nigerian he was having aGhanaian passport!  An individual has gotten by police on the grounds that being Nigerian he was having a Ghanaian visa! A 29-year-old Nigerian has been captured for endeavoring to get a visa with a falsely acquired Ghanaian travel permit."

【讨论】:

  • 但我想在我的 json 中换行。我该怎么办?
【解决方案3】:

感谢@MONTYHS 和@AvinashGarg 指出错误。换行导致 JSON 无效。要在我的 json 中添加换行符,我将所有换行标记 <br/> 替换为特殊字符,例如|~(小节和波浪号)。在客户端,解析 json 后,我将出现的 |~ 替换为 <br/> 以正确显示。

希望它可以帮助某人。参考是here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-21
    相关资源
    最近更新 更多