json值内的单引号

时间:2015-08-21 15:04:23

标签: javascript json formatting

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

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

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

var parsed = JSON.parse(json);

3 个答案:

答案 0 :(得分:12)

使用反斜杠escape the character

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

答案 1 :(得分:5)

使用反斜杠(例如\'

)转义单引号

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));

答案 2 :(得分:0)

以反斜杠

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

var parsed = JSON.parse(json);