2014-10-27 61 views
1

以下數據從MongoDB的錯誤而解析JSON的NodeJS

console.log(x) 

檢索到的輸出

{ _id: 54473495721e8a7386959897, 
    tag: 'java', 
    data: 
    [ 
    { view: '2342343', date: '2001/1/25' } 
    ] 
} 

在使用JSON.parse

var dataJson = JSON.parse(x); 

它thwors下面的錯誤解析

undefined:1 
{ _id: 54473495721e8a7386959897, 
^
SyntaxError: Unexpected token _ 
    at Object.parse (native) 
+1

看起來'x'已經是一個對象。不需要'JSON.parse' – Bulkan 2014-10-27 04:48:08

回答

1

空白給出了JSON錯誤更換第一

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

它已經是一個javascript對象,所以不需要解析,你想將它轉換爲json字符串嗎?

如果是你可以轉換成JSON與

JSON.stringify(x);