Javascript JSON.如果字符串后面有转义的反斜杠和大写字母,则分析错误



这个问题真的很简单。为什么我可以解析'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是一个特殊字符。

相关内容

最新更新