This question already has an answer here:
这个问题在这里已有答案:
- How can I accommodate a string with both single and double quotes inside of it in Javascript 4 answers
- 如何在Javascript 4答案中容纳其中包含单引号和双引号的字符串
- How to escape special characters in building a JSON string? 9 answers
- 如何在构建JSON字符串时转义特殊字符? 9个答案
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);