console.log 和 JSON.parse 返回错误

console.log and JSON.parse returning error

我正在尝试解析 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$s\u0022 class=\u0022widget %2$s\u0022\u003E","after_widget":"\u003C\/li\u003E\n","before_title":"\u003Ch2 class=\u0022widgettitle\u0022\u003E","after_title":"\u003C\/h2\u003E\n"}}'));</script>

这是 JSON,印刷精美:

{
    "header-top": {
        "name": "Header Top",
        "id": "header-top",
        "description": "",
        "class": "",
        "before_widget": "\u003Cli id=\u0022%1$s\u0022 class=\u0022widget %2$s\u0022\u003E",
        "after_widget": "\u003C\/li\u003E\n",
        "before_title": "\u003Ch2 class=\u0022widgettitle\u0022\u003E",
        "after_title": "\u003C\/h2\u003E\n"
    },
    "header": {
        "name": "Header",
        "id": "header",
        "description": "",
        "class": "",
        "before_widget": "\u003Cli id=\u0022%1$s\u0022 class=\u0022widget %2$s\u0022\u003E",
        "after_widget": "\u003C\/li\u003E\n",
        "before_title": "\u003Ch2 class=\u0022widgettitle\u0022\u003E",
        "after_title": "\u003C\/h2\u003E\n"
    },
    "header-bottom": {
        "name": "Header Bottom",
        "id": "header-bottom",
        "description": "",
        "class": "",
        "before_widget": "\u003Cli id=\u0022%1$s\u0022 class=\u0022widget %2$s\u0022\u003E",
        "after_widget": "\u003C\/li\u003E\n",
        "before_title": "\u003Ch2 class=\u0022widgettitle\u0022\u003E",
        "after_title": "\u003C\/h2\u003E\n"
    },
    "content-top": {
        "name": "Content Top",
        "id": "content-top",
        "description": "",
        "class": "",
        "before_widget": "\u003Cli id=\u0022%1$s\u0022 class=\u0022widget %2$s\u0022\u003E",
        "after_widget": "\u003C\/li\u003E\n",
        "before_title": "\u003Ch2 class=\u0022widgettitle\u0022\u003E",
        "after_title": "\u003C\/h2\u003E\n"
    },
    "content": {
        "name": "Content",
        "id": "content",
        "description": "",
        "class": "",
        "before_widget": "\u003Cli id=\u0022%1$s\u0022 class=\u0022widget %2$s\u0022\u003E",
        "after_widget": "\u003C\/li\u003E\n",
        "before_title": "\u003Ch2 class=\u0022widgettitle\u0022\u003E",
        "after_title": "\u003C\/h2\u003E\n"
    },
    "content-bottom": {
        "name": "Content Bottom",
        "id": "content-bottom",
        "description": "",
        "class": "",
        "before_widget": "\u003Cli id=\u0022%1$s\u0022 class=\u0022widget %2$s\u0022\u003E",
        "after_widget": "\u003C\/li\u003E\n",
        "before_title": "\u003Ch2 class=\u0022widgettitle\u0022\u003E",
        "after_title": "\u003C\/h2\u003E\n"
    },
    "footer-top": {
        "name": "Footer Top",
        "id": "footer-top",
        "description": "",
        "class": "",
        "before_widget": "\u003Cli id=\u0022%1$s\u0022 class=\u0022widget %2$s\u0022\u003E",
        "after_widget": "\u003C\/li\u003E\n",
        "before_title": "\u003Ch2 class=\u0022widgettitle\u0022\u003E",
        "after_title": "\u003C\/h2\u003E\n"
    },
    "footer": {
        "name": "Footer",
        "id": "footer",
        "description": "",
        "class": "",
        "before_widget": "\u003Cli id=\u0022%1$s\u0022 class=\u0022widget %2$s\u0022\u003E",
        "after_widget": "\u003C\/li\u003E\n",
        "before_title": "\u003Ch2 class=\u0022widgettitle\u0022\u003E",
        "after_title": "\u003C\/h2\u003E\n"
    },
    "footer-bottom": {
        "name": "Footer Bottom",
        "id": "footer-bottom",
        "description": "",
        "class": "",
        "before_widget": "\u003Cli id=\u0022%1$s\u0022 class=\u0022widget %2$s\u0022\u003E",
        "after_widget": "\u003C\/li\u003E\n",
        "before_title": "\u003Ch2 class=\u0022widgettitle\u0022\u003E",
        "after_title": "\u003C\/h2\u003E\n"
    }
}

这是因为您的其中一个字符串中的 \u0022 字符。

它的问题在于,当评估字符串文字(长 JSON)时,它被替换为 "(引号字符),这导致一些 JSON琴弦断了:

"\u0022"

变成

"""

所以从技术上讲,给定的 JSON 是有效的 JSON,但它不能在评估 \uXXXX 序列的环境中按原样使用。

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