eze*_*ero 12 javascript formatting json
Javascript无法读取此json字符串,因为它包含单引号字符,它将其视为字符串的结尾.
如何逃避单引号,以便它不被视为字符串的结尾?
var json = '{"1440167924916":{"id":1440167924916,"type":"text","content":"It's a test!"}}';
var parsed = JSON.parse(json);
Run Code Online (Sandbox Code Playgroud)
Cod*_*gue 13
使用反斜杠来转义字符:
var json = '{"1440167924916":{"id":1440167924916,"type":"text","content":"It\'s a test!"}}';
var parsed = JSON.parse(json);
Run Code Online (Sandbox Code Playgroud)
只需使用反斜杠来逃避单引号,例如\':
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));Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39465 次 |
| 最近记录: |