我有一个字符串,我想解码成json . 该字符串最初是base64 . 当我尝试解码为jason时,我得到以下错误 .
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:在回调时/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/app.js:115:16处的Object.parse(native)处的意外的令牌(/ 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) (/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 [作为句柄](/ home / ubuntu / workspace / rtbopsConfig / rtbServers / rtbNodejsServ er / node_modules / express / node_modules / connect / lib / middleware / methodOverride.js:35:5)at next(/ home / ubuntu / workspace / rtbopsConfig / rtbServers / rtbNodejsServer / node_modules / express / node_modules / connect / lib / http . JS:204:15)
2 回答
JSON格式需要 double quotes ,而不是 single quotes .
也:
None 应为: null
True 应为小写: true
query_string 应如下所示:
我猜这是一个Python字典,您应该使用库将python字典正确地序列化为JSON,或者如果您使用的是Python 2.6,只需执行以下操作:
文件:http://docs.python.org/library/json.html
JSON需要围绕键和(字符串)值的双引号,而不是单引号 .
也:
None
不是合法值 - 对空密钥进行编码的JSON方式是"mykey": null
True
和False
必须为小写JSON的正式语法位于首页http://www.json.org/