json值内的单引号[重复]

时间:2022-10-28 15:38:37

This question already has an answer here:

这个问题在这里已有答案:

Javascript fails to read this json string as it contains a single quote character which it sees as the end of the string.

Javascript无法读取此json字符串,因为它包含单引号字符,它将其视为字符串的结尾。

How can I escape the single quote so that it is not seen as the end of the string?

如何逃避单引号,以便它不被视为字符串的结尾?

var json = '{"1440167924916":{"id":1440167924916,"type":"text","content":"It's a test!"}}';

var parsed = JSON.parse(json);

3 个解决方案

#1


10  

Use a backslash to escape the character:

使用反斜杠来转义字符:

var json = '{"1440167924916":{"id":1440167924916,"type":"text","content":"It\'s a test!"}}';
var parsed = JSON.parse(json);

#2


5  

Just escape the single quote with a backslash such as \':

只需使用反斜杠(例如\')来逃避单引号:

var json = '{"1440167924916":{"id":1440167924916,"type":"text","content":"It\'s a test!"}}';

var parsed = JSON.parse(json);

//Output parsed to the document using JSON.stringify so it's human-readable and not just "[object Object]":
document.write(JSON.stringify(parsed));

#3


0  

Escape it with a backslash

用反斜杠逃脱它

var json = '{"1440167924916":{"id":1440167924916,"type":"text","content":"It\'s a test!"}}';

var parsed = JSON.parse(json);

#1


10  

Use a backslash to escape the character:

使用反斜杠来转义字符:

var json = '{"1440167924916":{"id":1440167924916,"type":"text","content":"It\'s a test!"}}';
var parsed = JSON.parse(json);

#2


5  

Just escape the single quote with a backslash such as \':

只需使用反斜杠(例如\')来逃避单引号:

var json = '{"1440167924916":{"id":1440167924916,"type":"text","content":"It\'s a test!"}}';

var parsed = JSON.parse(json);

//Output parsed to the document using JSON.stringify so it's human-readable and not just "[object Object]":
document.write(JSON.stringify(parsed));

#3


0  

Escape it with a backslash

用反斜杠逃脱它

var json = '{"1440167924916":{"id":1440167924916,"type":"text","content":"It\'s a test!"}}';

var parsed = JSON.parse(json);