这个问题真的很简单。为什么我可以解析'FOO\bAR'
而不能解析'FOO\BAR'
?
let sample = [
'{"a": "FOOBAR"}',
'{"b": "FOO\bAR"}',
'{"c": "FOO\\BAR"}',
'{"d": "FOO\BAR"}'
];
for(const item of sample) {
console.log(item);
console.log(JSON.parse(item));
}
结果:
{"a": "FOOBAR"}
{ a: 'FOOBAR' }
{"b": "FOObAR"}
{ b: 'FOObAR' }
{"c": "FOO\BAR"}
{ c: 'FOO\BAR' }
{"d": "FOOBAR"}
Uncaught SyntaxError: Unexpected token B in JSON at position 11
查找JSON特殊字符并对其进行转义,\b是一个特殊字符。