以下数据是从 MongoDB 检索的
console.log(x)
输出
{ _id: 54473495721e8a7386959897,
tag: 'java',
data:
[
{ view: '2342343', date: '2001/1/25' }
]
}
使用 JSON.parse 进行解析时
var dataJson = JSON.parse(x);
它抛出以下错误
undefined:1
{ _id: 54473495721e8a7386959897,
^
SyntaxError: Unexpected token _
at Object.parse (native)
最佳答案
JSON 中的空格会出错,请先替换它
x = x.replace(/\s/g, '');
x = JSON.stringify(x);
x = JSON.parse(x);
关于json - 解析 JSON Nodejs 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26581309/