console.log和JSON.parse返回错误

时间:2015-08-18 23:49:24

标签: javascript json google-chrome syntax console.log

我正在尝试解析JSON字符串并将其记录到Chrome控制台。字符串使用JSONLint进行验证。那么为什么Chrome会返回错误:"未捕获的SyntaxError:意外的令牌%"?

<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,Pretty Printed:

{
    "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"
    }
}

1 个答案:

答案 0 :(得分:1)

这是因为你的一个字符串中有\u0022字符。

它的问题在于,当对字符串文字(长JSON)进行求值时,它将被"(引号字符)替换,这会导致某些JSON字符串被破坏:

"\u0022"

变为

"""

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

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