未捕获的SyntaxError JSON.parse

时间:2017-06-08 14:26:41

标签: javascript jquery json

我正在尝试解析JSON字符串并抛出它 Uncaught SyntaxError : Unexpected token错误。下面是变量中的字符串。

  

注意:我检查了typeof()的jsonString变量,并返回string。所以变量是字符串类型,毫无疑问   该

JSON字符串:

{"title":"This is my Title","body":"<div class="ExternalClassB5F21AC89A1F40D0BCA5A8DBB6F738ED"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;">Our commitment to the development of this feature is
delayed  due to the unexpected JSON parse error.  </span></div>","byline":"This issue is killing me now for more than 2 days.  ","tile":0,"showText":true,"showImage":true,"image":{"ImageUrl":"/Path/to/imgFolder/hero%20background.png","Hyperlink":"","OpenHyperlinkInNewWindow":false,"Alignment":"","AlternateText":"","BorderWidth":0,"Height":0,"HorizontalSpacing":0,"VerticalSpacing":0,"Width":0},"publishedDate":"/Date(1496864514000)/","labels":["Character and Values"],"peopleList":[]}   

我无法确定问题是什么。有人有线索吗?

根据错误中的行号和列号,它显示意外字符位于第1行的结尾,即第1行的this feature is结尾。所以我可以猜到的是换行符可能是问题所在。即使换行导致,有没有办法处理这个解析问题。

提前致谢。

2 个答案:

答案 0 :(得分:2)

延迟后,您的JSON.body会有换行符。

 ..... feature is <--- THERE IS A LINEBREAK HERE
delayed  ..... 

这不是有效的JSON字符,应定义为 \ n

答案 1 :(得分:0)

您可以使用https://jsonlint.com/来验证您的JSON。使用固定换行符,它看起来像:

{
    "title": "This is my Title",
    "body": "&lt;div class=&quot;ExternalClassB5F21AC89A1F40D0BCA5A8DBB6F738ED&quot;&gt;&lt;span style=&quot;font-size&amp;#58;11.0pt;font-family&amp;#58;&amp;quot;Calibri&amp;quot;,sans-serif;&quot;&gt;Our commitment to the development of this feature is delayed due to the unexpected JSON parse error. & amp;#160;&lt;/span&gt;&lt;/div&gt;",
    "byline": "This issue is killing me now for more than 2 days.  ",
    "tile": 0,
    "showText": true,
    "showImage": true,
    "image": {
        "ImageUrl": "/Path/to/imgFolder/hero%20background.png",
        "Hyperlink": "",
        "OpenHyperlinkInNewWindow": false,
        "Alignment": "",
        "AlternateText": "",
        "BorderWidth": 0,
        "Height": 0,
        "HorizontalSpacing": 0,
        "VerticalSpacing": 0,
        "Width": 0
    },
    "publishedDate": "/Date(1496864514000)/",
    "labels": ["Character and Values"],
    "peopleList": []
}