Node.js不能parsing来自base64解码的JSONstring

我有一个string,我想解码成json。 该string最初是base64。 当我尝试解码成杰森我得到下面的错误。

var query_string = new Buffer(bid, 'base64').toString('ascii'); console.log(query_string); var q = JSON.parse(query_string); {'avid': 'info@tssf.co.jp', 'crid': '20767073515', 'mabid': {'node': None, 'hod': '13', 'cid': '36', 'industry': None, 'ex': '1', 'vid1': '29', 'dow': '3'}, 'prid': {'hod': '13', 'woy': '18', 'cid': '36', 'dow': '3', 'ssp': 'adx', 'st': None, 'bt': 'firefox', 'cty': 'tokyo', 'ex': '1', 'vid2': '222', 'dt': '1', 'os': 'mac', 'vid1': '29'}, 'agid': '4547917795', 'cookieid': 'retageting:cookie', 'did': 'yahoo.com', 'validation': True} 

SyntaxError:Unexpected token'Object.parse(native)at /home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/app.js:115:16 at callbacks(/ home / ubuntu / workspace / rtbopsConfig / rtbServers / rtbNodejsServer / node_modules /express/lib/router/index.js:272:11)at param(/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/lib/router/index.js:246:11)at pass( /home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/lib/router/index.js:253:5)在Router._dispatch(/ home / ubuntu / workspace / rtbopsConfig / rtbServers / rtbNodejsServer / node_modules / express /lib/router/index.js:280:4)在Object.handle(/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/lib/router/index.js:45:10) /home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/node_modules/connect/lib/http.js:204:15)在Object.methodOverride [as handle](/ home / ubuntu / workspace / rtbopsConfig / rtbServers / rtbNodejsServer /node/modules/express/node_modules/connect/lib/middleware/methodOverride.js:35:5)(/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/node_modules/connect/lib/http.js :204:15)

JSON格式需要双引号 ,而不是单引号

也:

  • 应该是null
  • 应该是小写:

query_string应该如下所示:

 {"avid": "info@tssf.co.jp", "crid": "20767073515", "mabid": {"node": null, "hod": "13", "cid": "36", "industry": null, "ex": "1", "vid1": "29", "dow": "3"}, "prid": {"hod": "13", "woy": "18", "cid": "36", "dow": "3", "ssp": "adx", "st": null, "bt": "firefox", "cty": "tokyo", "ex": "1", "vid2": "222", "dt": "1", "os": "mac", "vid1": "29"}, "agid": "4547917795", "cookieid": "retageting:cookie", "did": "yahoo.com", "validation": true} 

我想这是一个Python字典,你应该使用一个库来正确地序列化一个Python字典到JSON,或者如果你使用Python 2.6+,只需要:

 import json json_string = json.dumps({'test': 'test'}) 

文档: http : //docs.python.org/library/json.html

JSON需要围绕键和(string)值使用双引号,而不是单引号。

也:

  1. None一个不是合法的值 – 编码空键的JSON方法是"mykey": null
  2. TrueFalse必须小写

JSON的正式语法位于http://www.json.org/的头版