使用嵌套字典和列表在 python 中创建一个 json 对象
Create a json object in python with nested dictionary and list
我正在尝试在 python 中创建以下负载以发出 POST 请求。以下行来自我浏览器中的 REST 客户端并创建了我想要的结果,我知道它有效。
(URL 编码的表单数据)
features=[{"attributes":{"POINT_NAME":"tpoint","EASTING":"338250","NORTHING":"342300","CREATION_DATE":"16/12/ 2020",},"几何":{"x":338250,"y":342300}}]&f=json
到目前为止,我进行了多次尝试,最接近的尝试如下:
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
有效负载输出为:
{'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":["解析器错误:无法识别某些参数。"]}}'
任何想法都会有所帮助,我被卡住了。
谢谢
复制评论中的答案:尝试使用 payload as
payload = {"features": json.dumps(data_list) , "f" : "json"}
我正在尝试在 python 中创建以下负载以发出 POST 请求。以下行来自我浏览器中的 REST 客户端并创建了我想要的结果,我知道它有效。
(URL 编码的表单数据) features=[{"attributes":{"POINT_NAME":"tpoint","EASTING":"338250","NORTHING":"342300","CREATION_DATE":"16/12/ 2020",},"几何":{"x":338250,"y":342300}}]&f=json
到目前为止,我进行了多次尝试,最接近的尝试如下:
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
有效负载输出为: {'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":["解析器错误:无法识别某些参数。"]}}'
任何想法都会有所帮助,我被卡住了。
谢谢
复制评论中的答案:尝试使用 payload as
payload = {"features": json.dumps(data_list) , "f" : "json"}