我想从REST请求中获取json响应数据,并创建一个实际的json文件。我尝试了类似的方法,但是没有用。本质上,它只是打印标题。有什么建议吗?
params = {'f': 'json', 'where': '1=1', 'geometryType': 'esriGeometryPolygon', 'spatialRel': 'esriSpatialRelIntersects','outFields': '*', 'returnGeometry': 'true'}
r = requests.get('https://hazards.fema.gov/gis/nfhl/rest/services/CSLF/Prelim_CSLF/MapServer/3/query', params)
cslfJson = r.json()
path = r"C:/Workspace/Sandbox/ScratchTests/cslf.json"
file = open(path, 'w')
for line in cslfJson:
file.write(line + "\r\n")
file.close()
答案 0 :(得分:1)
使用json
模块
my_data = json.loads(r.json())
# my_data is a dict mapping the JSON
with open(path, 'w') as f:
json.dump(my_data, f)
如果需要,可以使用缩进参数以漂亮的方式打印
json.dump(my_data, f, indent=2)