python - 字符串索引必须是整数 json
问题描述
我有一个 json 数据https://steamcommunity.com/id/RednelssGames/inventory/json/730/2 需要获取所有项目的名称
r = requests.get('https://steamcommunity.com/id/RednelssGames/inventory/json/730/2')
if r.json()['success'] == True:
for rows in r.json()['rgDescriptions']:
print(rows['market_hash_name'])
获取错误字符串索引必须是整数
解决方案
从您提供的链接:
"rgDescriptions":{"4291220570_302028390":
rgDescriptions
不返回数组,而是一个对象(在本例中为字典)(注意左大括号 ( {
) 而不是常规方括号 ( [
))。
通过使用for rows in r.json()['rgDescriptions']:
,您最终会遍历字典的键。字典的第一个键似乎是"4291220570_302028390"
,它是一个字符串。
所以当你这样做时print(rows['market_hash_name'])
,你试图访问'market_hash_name'
你的对象的“索引” rows
,但rows
实际上是一个字符串,所以它不起作用。
推荐阅读
- bigdata - Star Schema(数据建模)是否仍然与使用 Databricks 的 Lake House 模式相关?
- python - 使用服务主体访问 Azure Blob 存储但收到错误“无法获取本地颁发者证书”
- r - R方法提取像素数据
- sql-server - 将文本文件导入 SSMS - 停止将日期时间字段拆分为 2 列
- sql - 使用 Simba ODBC 驱动程序从 AWS Athena 查询特定十进制值失败
- bash - 使用 bash 从根据请求生成文件的链接下载 tsv 文件
- oracle - Web 源模块 POST 请求 Oracle APEX 的问题
- c - C语言中的Havel-Hakimi算法
- mysql - 对远程数据库 mariadb 的查询比在同一台服务器上的查询要慢
- .net - 在 .net 核心中使用 dapper 进行递归查询