json - 解析 JSON Nodejs 时出错

标签 json node.js mongodb

以下数据是从 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/

相关文章:

c# - Windows Azure 移动服务和序列化遇到困难

c# - 解析来自 Httpwebrequest 的 Json 响应

javascript - 使用模型存储在关卡中的数据中的某些字段不会被持久化

node.js - 如何修复 Node.js 中 Oauth 2.0 的 'invalid_client' 错误

objective-c - ObjCMongoDB 身份验证

javascript - 如何使用JS解析JSON字符串中的参数?

java - 一个接受 JSON 对象的简单 JaxWS Rest

node.js - 使用 Jenkins 在 NodeJS 上运行单元测试

node.js - 如何通过 Express Route 使用 Mongoose-Crate 存储文件

MongoDB .msi 安装程序无法在 Windows 10 上运行