问题
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