console.log and JSON.parse returning error

I am attempting to parse a JSON string and log it to the Chrome console. The string validates using JSONLint. Why then is Chrome is returning the error: "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>

Here is the 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"
    }
}

That happens because of the u0022 character in one of your strings.

The problem with it is that when the string literal (the long JSON) is evaluated it's being replaced with " (a quote character), which leads to some JSON string becoming broken:

"u0022"

becomes

"""

So technically the given JSON is a valid JSON, but it cannot be used as-is in an environment that evaluates uXXXX sequences.

So you could, for example, read it from some other source - eg as a response from AJAX request, but you cannot use it as a string literal in JS code.

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

上一篇: 未捕获的jquery SyntaxError:意外的标记:

下一篇: console.log和JSON.parse返回错误