简体   繁体   中英

Technique to avoid this parse error? JSON +double quote

We know in JSON string object we have property and its value as “Property”:”Value”

Suppose my Value contains a double quote, Like “Property”: “my country is “uk” ”

We know that this is going to give parse error on JSON.parse().

What is the technique to avoid this parse error?

You can use backslash to escape the double qoute, read more about escape character over here .

Change

“my country is “uk” ”

To

“my country is \“uk\” ”

If you're encoding an object into JSON, you can use JSON.stringify() :

JSON.stringify({
    Property: 'my country is "uk"'
})
// {"Property":"my country is \"uk\""}

As you can see from above example, the notation \\" is used to properly escape double quotes.

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