简体   繁体   English

python将dict转换为JSON

[英]python converting dict to JSON

I have a python dict in the following format 我有以下格式的python字典

{'status': ['Done'], 'urgency': 1, 'text': {'shorttext': 'Short Text', 'longtext': 'Long Text'}, 'startdate': '2019-03-03', 'enddate': '2019-03-03'}

which when I convert to json using json_dumps 当我使用json_dumps转换为json时

obj=json_dumps(dict)
print(obj)

'{"status": ["Done"], "urgency": 1, "text": {"shorttext": "Short Text", "longtext": "Long Text"}, "startdate": "2019-03-03", "enddate": "2019-03-03"}'

Now when I try to post this payload to an api using request.post call in the format below 现在,当我尝试使用request.post调用以以下格式将有效负载发布到api时

requests.post(url, headers, json=obj)

I get the below error 我收到以下错误

no String-argument constructor/factory method to deserialize from String value (\'{"status": ["Done"], "urgency": 1, "text": {"shorttext": "Short Text", "longtext": "Long Text"}, "startdate": "2019-03-03", "enddate": "2019-03-03"}'\)

Any inputs on what might be causing this? 关于什么可能导致此的任何输入? I suspect is the '' the payload is enclosed in, but am not completely sure. 我怀疑是有效载荷包含在其中,但不确定。 Thanks! 谢谢!

Don't dump the the dict! 不要放弃该命令!

requests.post(url, headers, json=dict)

Or, if you have to: 或者,如果您必须:

requests.post(url, headers, data=obj)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM