javascript - Node.js 无法从 base64 解码解析 JSON 字符串

标签 javascript json node.js base64

我有一个字符串,我想解码成 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: Unexpected token ' at 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) at Router._dispatch (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/lib/router/index.js:280:4) at Object.handle (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/lib/router/index.js:45:10) at next (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/node_modules/connect/lib/http.js:204:15) at Object.methodOverride [as handle] (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/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)

最佳答案

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

还有:

  • None 应该是:null
  • True 应小写:true

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

关于javascript - Node.js 无法从 base64 解码解析 JSON 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10717033/

相关文章:

javascript - 将相同的事件处理代码附加到 jquery 中的多个事件

javascript - 使用变量作为国家/地区键动态更新数据图填充颜色不起作用

javascript - 在项目启动时从 $http.GET 设置 Angularjs 常量

javascript - ExtJS 显示对象属性

javascript - 如何在 node.js 中重定向时将数据从 express 传递到 .ejs 文件

javascript - ReactJS 身份验证不起作用,重定向到登录页面失败

javascript - 无法限制 RTCMulticonnection 中的带宽使用

json - 在 Postgres 中从 JSON 获取嵌套对象值

node.js - 使用 Node.js 的 Express 进行 SEO

javascript - Mongoose post.save在heroku上失败,在本地主机上工作