console.log和JSON.parse返回错误

我试图解析JSON字符串并将其记录到Chrome控制台。 该字符串使用JSONLint进行验证。 那为什么Chrome会返回错误:“Uncaught SyntaxError:Unexpected token%”?

<script>console.log(JSON.parse('{"header-top":{"name":"Header Top","id":"header-top","description":"","class":"","before_widget":"u003Cli id=u0022%1$su0022 class=u0022widget %2$su0022u003E","after_widget":"u003C/liu003En","before_title":"u003Ch2 class=u0022widgettitleu0022u003E","after_title":"u003C/h2u003En"}}'));</script>

这是JSON,Pretty Printed:

{
    "header-top": {
        "name": "Header Top",
        "id": "header-top",
        "description": "",
        "class": "",
        "before_widget": "u003Cli id=u0022%1$su0022 class=u0022widget %2$su0022u003E",
        "after_widget": "u003C/liu003En",
        "before_title": "u003Ch2 class=u0022widgettitleu0022u003E",
        "after_title": "u003C/h2u003En"
    },
    "header": {
        "name": "Header",
        "id": "header",
        "description": "",
        "class": "",
        "before_widget": "u003Cli id=u0022%1$su0022 class=u0022widget %2$su0022u003E",
        "after_widget": "u003C/liu003En",
        "before_title": "u003Ch2 class=u0022widgettitleu0022u003E",
        "after_title": "u003C/h2u003En"
    },
    "header-bottom": {
        "name": "Header Bottom",
        "id": "header-bottom",
        "description": "",
        "class": "",
        "before_widget": "u003Cli id=u0022%1$su0022 class=u0022widget %2$su0022u003E",
        "after_widget": "u003C/liu003En",
        "before_title": "u003Ch2 class=u0022widgettitleu0022u003E",
        "after_title": "u003C/h2u003En"
    },
    "content-top": {
        "name": "Content Top",
        "id": "content-top",
        "description": "",
        "class": "",
        "before_widget": "u003Cli id=u0022%1$su0022 class=u0022widget %2$su0022u003E",
        "after_widget": "u003C/liu003En",
        "before_title": "u003Ch2 class=u0022widgettitleu0022u003E",
        "after_title": "u003C/h2u003En"
    },
    "content": {
        "name": "Content",
        "id": "content",
        "description": "",
        "class": "",
        "before_widget": "u003Cli id=u0022%1$su0022 class=u0022widget %2$su0022u003E",
        "after_widget": "u003C/liu003En",
        "before_title": "u003Ch2 class=u0022widgettitleu0022u003E",
        "after_title": "u003C/h2u003En"
    },
    "content-bottom": {
        "name": "Content Bottom",
        "id": "content-bottom",
        "description": "",
        "class": "",
        "before_widget": "u003Cli id=u0022%1$su0022 class=u0022widget %2$su0022u003E",
        "after_widget": "u003C/liu003En",
        "before_title": "u003Ch2 class=u0022widgettitleu0022u003E",
        "after_title": "u003C/h2u003En"
    },
    "footer-top": {
        "name": "Footer Top",
        "id": "footer-top",
        "description": "",
        "class": "",
        "before_widget": "u003Cli id=u0022%1$su0022 class=u0022widget %2$su0022u003E",
        "after_widget": "u003C/liu003En",
        "before_title": "u003Ch2 class=u0022widgettitleu0022u003E",
        "after_title": "u003C/h2u003En"
    },
    "footer": {
        "name": "Footer",
        "id": "footer",
        "description": "",
        "class": "",
        "before_widget": "u003Cli id=u0022%1$su0022 class=u0022widget %2$su0022u003E",
        "after_widget": "u003C/liu003En",
        "before_title": "u003Ch2 class=u0022widgettitleu0022u003E",
        "after_title": "u003C/h2u003En"
    },
    "footer-bottom": {
        "name": "Footer Bottom",
        "id": "footer-bottom",
        "description": "",
        "class": "",
        "before_widget": "u003Cli id=u0022%1$su0022 class=u0022widget %2$su0022u003E",
        "after_widget": "u003C/liu003En",
        "before_title": "u003Ch2 class=u0022widgettitleu0022u003E",
        "after_title": "u003C/h2u003En"
    }
}

发生这种情况是因为你的字符串中有一个“ u0022字符。

它的问题是,当字符串文字(长JSON)被评估时,它被替换为" (引号字符)”,这导致某些JSON字符串变得中断:

"u0022"

"""

所以从技术上讲,给定的JSON是一个有效的JSON,但它不能用于评估uXXXX序列的环境中。

例如,您可以从其他来源读取它 - 例如,作为来自AJAX请求的响应,但不能将其用作JS代码中的字符串字面值。

链接地址: http://www.djcxy.com/p/45949.html

上一篇: console.log and JSON.parse returning error

下一篇: $.parseJSON giving "Uncaught SyntaxError: Unexpected token"