【问题标题】:Error while parsing JSON nodejs解析 JSON nodejs 时出错
【发布时间】:2014-10-27 15:31:21
【问题描述】:

以下数据取自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)

【问题讨论】:

  • 看起来 x 已经是一个对象。不需要JSON.parse

标签: json node.js mongodb


【解决方案1】:

空格在 JSON 中给出错误,首先替换它

x = x.replace(/\s/g, '');
x = JSON.stringify(x);
x = JSON.parse(x);

【讨论】:

    【解决方案2】:

    它已经是一个javascript对象所以不需要解析,你想把它转换成json字符串吗?

    如果是,您可以使用

    转换为 JSON
      JSON.stringify(x);
    

    【讨论】:

      猜你喜欢
      • 2023-03-21
      • 2018-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-07
      相关资源
      最近更新 更多