【发布时间】:2019-07-26 15:40:55
【问题描述】:
我正在尝试将从 API 接收到的数据保存在 json 格式的文件中。
response = requests.get(url_league, headers= header)
type(response)
#output requests.models.Response
with open("json.txt", "w+") as f:
data = json.dump(response, f)
当我尝试将响应对象保存到文件时,出现以下错误
Object of type Response is not JSON serializable
我读到 json 模块在编码复杂对象方面存在问题,为此目的,json 具有编码复杂对象的默认函数。我尝试了以下代码
json_data = json.dump(response.__dict__, f, default = lambda o: o.__dict__, indent=4)
并得到以下错误
bytes' object has no attribute '__dict__'
这个错误是什么意思以及如何解决?
【问题讨论】:
-
你试过使用
response.json()吗? -
Wonderfull) 我试过了,得到了我想要的。谢谢大佬
标签: python json python-requests