python - 对象没有属性 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'
解决方案
这是因为它Json
是 python 中的一个库/包。
symbols.json.loads() # wrong statement
为了使用 json 你必须首先在你的文件中导入 json packagegae
import json
json.loads(symbols); # you have use statement like this.
您必须将变量或值传递给loads()
函数。
推荐阅读
- tensorflow - 张量流中二元交叉熵的标签平滑
- java - 使用springboot从firebase实时数据库中读取数据
- powershell - 使用 PowerShell 连接到 Exchange 2019 EWS 时出错
- reactjs - 无法在 URLInputButton 中输入文本 (Gutenberg)
- javascript - 如何设置此 JS 代码以进行更好的测试?
- r - ggplot2 geom_point 仅当 y 值不同于 0 时才具有颜色
- docker - 在 Dockerfiles 中动态注册环境变量
- python - python中的基类
- javascript - Sharepoint Modern Site - 页面加载后调用 JS 函数
- .net-core - 如何使用 Visual Studio 2019 在 Hyper-V Linux VM 上远程调试 .NetCore 应用程序?