json.dump后面的参数 separators的含义

如果遇到参数请求没有返回结果,很可能是参数格式问题,可以用以下方法处理
data=json.dumps(params, separators=())

例1

import json
json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':'))

返回结果
'[1,2,3,{"4":5,"6":7}]'

例2

import json
params = {
           "accountID": "4807ae96-fec9-a14c-e5a6-d57d898d3c21",
          "digitalSign": f"{digitalSign}",
          "reqTime": "1631242383970",
          "serviceName": "mkt-user",
          "version": "20181113154313"
}
print("格式处理前",params)
data = json.dumps(params, separators=(",", ":"))
print("格式处理后",data)

返回结果:
格式处理前 {'accountID': '4807ae96-fec9-a14c-e5a6-d57d898d3c21', 'digitalSign': 'cd778fdec5d892dab2bdac3acbb6da8b', 'reqTime': '1631242383970', 'serviceName': 'mkt-user', 'version': '20181113154313'}

返回结果:
格式处理后 {"accountID":"4807ae96-fec9-a14c-e5a6-d57d898d3c21","digitalSign":"cd778fdec5d892dab2bdac3acbb6da8b","reqTime":"1631242383970","serviceName":"mkt-user","version":"20181113154313"}

处理后和之前请求参数一样有 ""

posted @ 2021-09-10 17:19  莫贞俊晗  阅读(1085)  评论(0编辑  收藏  举报