Invalid Json Error in JsonLint

蹲街弑〆低调 提交于 2019-12-25 17:00:11

问题


I have a web service returning following 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."
    }
]

JSONLint is giving following error:

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

I am really unable to understand what is making json invalid here? Is it newline or what? What can I do to make it work? Thanks.


回答1:


New line present in introtex, this is valid json check it

         [
        {
            "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."
        }
    ]



回答2:


Put your whole string in one line. example

[
{
    "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."
}

]

In case you want line break use

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



回答3:


Thanks to @MONTYHS and @AvinashGarg for pointing out the mistake. Newline is causing invalid JSON. To have break lines in my json, I am replacing all newline tags <br/> with a special character, e.g. |~ (bar and a tilde). At client side, after parsing json, I replace occurrences of |~ with <br/> back to display properly.

Hope it helps someone. Reference is here.



来源:https://stackoverflow.com/questions/21675038/invalid-json-error-in-jsonlint

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!