2012-06-16 79 views
1
data={"convID":"E40E92FD4B410","serverDate":"2012/06/15 07:51:07 PM","masterInfo":{"instance":"808477149_R_BDD72269D1C1E98A32D8C83EC58CF471","appVersion":"0.97.13.D","depth":4,"jsonURL":"//testtn","scenario":"R","serverDate":"2012/06/15 07:51:07 PM ","deviceName":"M’s","localDate":"2012/5/6 11:14:59 AM PDT"}} 

JSON.parse(data); 

給出錯誤。這是由ruby腳本中的JSON.generate生成的,併發送給節點http服務器,應用decodeURIComponent獲取上面打印的數據。還是JSON解析失敗,此錯誤:甚至在解碼後,node.js中的JSON解析錯誤URL

undefined:1 
ate":"2012/06/15 
^ 
SyntaxError: Unexpected token d 
at Object.parse (native) 

回答

3

在你的代碼示例data是JS對象。但是JSON.parse以字符串作爲參數,而不是對象。所以,你的代碼必須是:

data='{"convID":"E40E92FD4B410","serverDate":"2012/06/15 07:51:07 PM","masterInfo":{"instance":"808477149_R_BDD72269D1C1E98A32D8C83EC58CF471","appVersion":"0.97.13.D","depth":4,"jsonURL":"//testtn","scenario":"R","serverDate":"2012/06/15 07:51:07 PM ","deviceName":"M’s","localDate":"2012/5/6 11:14:59 AM PDT"}}'; 

JSON.parse(data); 

所以,檢查值和data變量您的真實代碼的類型。

+0

謝謝,這個提示幫了我。 – user1447121

+0

好。你應該[接受](http://meta.stackexchange.com/a/5235/188359)我的答案。 –