即使解码URL后,node.js中的JSON解析错误

时间:2012-06-16 02:55:48

标签: json parsing node.js

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);

给出错误。这是由JSON.generate在ruby脚本中生成并发送到节点http服务器,decodeURIComponent用于获取上面打印的数据。仍然JSON解析失败并出现此错误:

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

1 个答案:

答案 0 :(得分: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变量的值和类型。