首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JsonLint中的无效Json错误

JsonLint中的无效Json错误
EN

Stack Overflow用户
提问于 2014-02-10 18:46:08
回答 3查看 714关注 0票数 0

我有一个web服务返回下面的json:

代码语言:javascript
复制
[
    {
        "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显示以下错误:

代码语言:javascript
复制
Parse error on line 5:
...       "introtext": "A person has grabbe
-----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['

我真的不能理解是什么让json在这里无效?是换行符还是怎么的?我能做些什么来让它工作呢?谢谢。

EN

回答 3

Stack Overflow用户

发布于 2014-02-10 18:48:35

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

代码语言:javascript
复制
         [
        {
            "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."
        }
    ]
票数 1
EN

Stack Overflow用户

发布于 2014-02-10 18:51:47

将您的整个字符串放在一行中。示例

代码语言:javascript
复制
[
{
    "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."
}

]

如果您需要换行符,请使用

代码语言:javascript
复制
"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."
票数 0
EN

Stack Overflow用户

发布于 2014-02-10 19:32:41

感谢@MONTYHS和@AvinashGarg指出错误。换行符导致无效的JSON。为了在我的json中换行,我用一个特殊的字符,例如|~ (bar和波浪号)替换了所有的换行符<br/>。在客户端,在解析json之后,我用<br/>替换了出现的|~,以便正确显示。

希望这能帮助到别人。引用是here

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21675038

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档