Json:TypeError: Object of type Response is not JSON serializable

# 省略...
res = requests.post(url=url,json=data)
print("total time: {} sec".format(time.time()-t))
print(res.text)
filename='/path/A.json'
e = json.dumps(res, ensure_ascii=False, indent=4)
with open(filename, "w") as file_obj:
    file_obj.write(e)

File "/usr/python3.7/lib/python3.7/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Response is not JSON serializable

Solution: add res = res.json()

# 省略...
res = requests.post(url=url,json=data)
print("total time: {} sec".format(time.time()-t))
print(res.text)
filename='/path/A.json'
res = res.json()
e = json.dumps(res, ensure_ascii=False, indent=4)
with open(filename, "w") as file_obj:
    file_obj.write(e)

Guess you like

Origin blog.csdn.net/weixin_40437821/article/details/113524937