[英]Create a json object in python with nested dictionary and list
I am trying to create in python the following payload in order to make a POST request.我正在尝试在 python 中创建以下有效负载以发出 POST 请求。 The following line comes from the REST client in my browser and creates the result I want, and I know it works.
以下行来自我浏览器中的 REST 客户端并创建了我想要的结果,我知道它可以工作。
(URLencoded form data) features=[{"attributes":{"POINT_NAME":"tpoint","EASTING":"338250","NORTHING":"342300","CREATION_DATE":"16/12/2020",},"geometry":{"x":338250,"y":342300}}]&f=json (URLencoded 表单数据) features=[{"attributes":{"POINT_NAME":"tpoint","EASTING":"338250","NORTHING":"342300","CREATION_DATE":"16/12/2020", },"几何":{"x":338250,"y":342300}}]&f=json
So far, I did a number of attempts with the closest being the following:到目前为止,我做了很多尝试,最接近的是以下几种:
import json
import requests
#Create inner dictionary
data_dictionary = {}
data_dictionary['attributes'] = {"POINT_NAME" : "tpoint", "EASTING" : "338250", "NORTHING" : "342300", "CREATION_DATE" : "16/12/2020"}
data_dictionary['geometry'] ={"x" : 338250, "y" : 342300}
data_list = [data_dictionary]
payload = {"features": data_list , "f" : "json"}
r = request.post(API_ENDPOINT_POINT, data = payload)
r.text
payload output is: {'features': [{'attributes': {'POINT_NAME': 'tpoint', 'EASTING': '338250', 'NORTHING': '342300','CREATION_DATE': '16/12/2020'},'geometry': {'x': 338250, 'y': 342300}}], 'f': 'json'}, but when I make the request I get: '{"error":{"code":500,"message":"Unable to complete operation.","details":["Parser error: Some parameters could not be recognized."]}}'有效载荷 output 是:{'features':[{'attributes':{'POINT_NAME':'tpoint','EASTING':'338250','NORTHING':'342300','CREATION_DATE':'16/12/2020 '},'geometry': {'x': 338250, 'y': 342300}}], 'f': 'json'},但是当我提出请求时,我得到:'{"error":{"code ":500,"message":"无法完成操作。","details":["解析器错误:无法识别某些参数。"]}}'
Any ideas would be helpful, I am stuck.任何想法都会有所帮助,我被卡住了。
Thanks谢谢
Copying the answer from comments: try to use payload as从评论中复制答案:尝试使用有效负载作为
payload = {"features": json.dumps(data_list) , "f" : "json"}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.