首页 > 解决方案 > 对象没有属性 JSON

问题描述

我正在尝试将 API 的输出打印到本地 json 文件中,以便可以搜索该文件。

我尝试查看类型symbols以查看对象类型是什么,但没有任何运气。在静态类型语言中,由于明确知道类型是什么,我可以更轻松地进行故障排除。有没有解决此类问题的好方法?

symbols = urllib.request.urlopen("https://cloud.iexapis.com/stable/ref-data/symbols?format=json&token={}".format(key))

symbols.json.loads()
with open('data.json', 'w', encoding='utf-8') as f:
    json.dump(symbols, f, ensure_ascii=False, indent=4)

print(type(symbols))

我得到的错误是:

AttributeError: 'HTTPResponse' object has no attribute 'json'

标签: pythonjson

解决方案


这是因为它Json是 python 中的一个库/包。

symbols.json.loads() # wrong statement

为了使用 json 你必须首先在你的文件中导入 json packagegae

import json
json.loads(symbols); # you have use statement like this.

您必须将变量或值传递给loads()函数。


推荐阅读