简体   繁体   中英

Error in parsing json Expecting '{', '['**

This is the json .

"{
    'places': [
        {
            'name': 'New\x20Orleans,
            \x20US\x20\x28New\x20Lakefront\x20\x2D\x20NEW\x29',
            'code': 'NEW'
        }

    ]
}"

I am getting json parsererror. I am checking on http://jsonlint.com/ and it shows following error

Parse error on line 1:
"{    'places': [ 
^
Expecting '{', '['

Please explain what are the problems with the json and do I correct it?

If you literally mean that the string, as a whole, is your JSON text (containing something that isn't JSON), there are three issues:

  1. It's just a JSON fragment , not a full JSON document.

  2. Literal line breaks within strings are not valid in JSON, use \\n .

  3. \\x is an invalid escape sequence in JSON strings. If you want your contained non-JSON text to have a \\x escape (eg, when you read the value of the overall string and parse it), you have to escape that backslash: \\\\x .

In a full JSON document, the top level must be an object or array:

{"prop": "value"}

[1, 2, 3]

Most JSON parsers support parsing fragments , such as standalone strings. (For instance, JavaScript's JSON.parse supports this.) http://jsonlint.com is doing full document parsing, however.

Here's your fragment wrapped in an object with the line breaks and \\x issue handled:

{
    "stuff": "{\n 'places': [\n {\n 'name': 'New\\x20Orleans,\n \\x20US\\x20\\x28New\\x20Lakefront\\x20\\x2D\\x20NEW\\x29',\n 'code': 'NEW'\n }\n \n ]\n }"
}

The text within the string is also not valid JSON, but perhaps it's not meant to be. For completeness: JSON requires that all keys and strings be in double quotes ( " ), not single quotes ( ' ). It also doesn't allow literal line breaks within string literals (use \\n instead), and doesn't support \\x escapes. See http://json.org for details.

Here's a version as valid JSON with the \\x converted to the correct JSON \\u\u003c/code> escape:

{
    "places": [
        {
            "name": "New\u0020Orleans,\n\u0020US\u0020\u0028New\u0020Lakefront\u0020\u002D\u0020NEW\u0029",
            "code": "NEW"
        }
    ]
}

...also those escapes are all actually defining perfectly normal characters, so:

{
    "places": [
        {
            "name": "New Orleans,\n US (New Lakefront - NEW)",
            "code": "NEW"
        }
    ]
}

read http://json.org/

{
    "places": [
        {
            "name": "New\\x20Orleans,\\x20US\\x20\\x28New\\x20Lakefront\\x20\\x2D\\x20NEW\\x29",
            "code": "NEW"
        }
    ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM