在 Python 中访问复杂的 JSON 数据
我尝试访问复杂的 JSON 数据结构,但出现此错误:
TypeError: listindexs Must be integers or slices, not str
这是我正在使用的 JSON 数据响应:
data = [
{
"id": "matic_atlantis",
"chain": "matic",
"name": "Atlantis",
"site_url": "https://atlantis.loans",
"logo_url": "https://static.debank.com/image/project/logo_url/matic_atlantis/a1334c12971c12f4796ea439fbc1c5f8.png",
"has_supported_portfolio": True,
"tvl": 0,
"portfolio_item_list": [
{
"stats": {
"asset_usd_value": 20.258310032314448,
"debt_usd_value": 0.03452806117121324,
"net_usd_value": 20.223781971143236
},
"update_at": 1645959230.678967,
"name": "Lending",
"pool_id": "0x8f85ee1c0a96734cb76870106dd9c016db6de09a",
"detail_types": [
"lending"
],
"detail": {
"supply_token_list": [
{
"id": "matic",
"chain": "matic",
"name": "MATIC",
"symbol": "MATIC",
"display_symbol": None,
"optimized_symbol": "MATIC",
"decimals": 18,
"logo_url": "https://static.debank.com/image/matic_token/logo_url/matic/e5a8a2860ba5cf740a474dcab796dc63.png",
"protocol_id": "",
"price": 1.525962,
"is_verified": True,
"is_core": True,
"is_wallet": True,
"time_at": None,
"amount": 0.00007212821116324932,
"is_collateral": True
},
{
"id": "0x831753dd7087cac61ab5644b308642cc1c33dc13",
"chain": "matic",
"name": "Quickswap",
"symbol": "QUICK",
"display_symbol": None,
"optimized_symbol": "QUICK",
"decimals": 18,
"logo_url": "https://static.debank.com/image/matic_token/logo_url/0x831753dd7087cac61ab5644b308642cc1c33dc13/3f40a8915d99b9dd0e24d5205c89eb34.png",
"protocol_id": "matic_quickswap",
"price": 165.3,
"is_verified": True,
"is_core": True,
"is_wallet": True,
"time_at": 1602175296,
"amount": 0.12255414378345482,
"is_collateral": True
}
],
"borrow_token_list": [
{
"id": "matic",
"chain": "matic",
"name": "MATIC",
"symbol": "MATIC",
"display_symbol": None,
"optimized_symbol": "MATIC",
"decimals": 18,
"logo_url": "https://static.debank.com/image/matic_token/logo_url/matic/e5a8a2860ba5cf740a474dcab796dc63.png",
"protocol_id": "",
"price": 1.525962,
"is_verified": True,
"is_core": True,
"is_wallet": True,
"time_at": None,
"amount": 0.000002603921715322
},
{
"id": "0x53e0bca35ec356bd5dddfebbd1fc0fd03fabad39",
"chain": "matic",
"name": "ChainLink Token",
"symbol": "LINK",
"display_symbol": None,
"optimized_symbol": "LINK",
"decimals": 18,
"logo_url": "https://static.debank.com/image/matic_token/logo_url/0x53e0bca35ec356bd5dddfebbd1fc0fd03fabad39/69425617db0ef93a7c21c4f9b81c7ca5.png",
"protocol_id": "",
"price": 14.5811,
"is_verified": True,
"is_core": True,
"is_wallet": True,
"time_at": 1598767775,
"amount": 0.000163336006690615
},
{
"id": "0x2791bca1f2de4661ed88a30c99a7a9449aa84174",
"chain": "matic",
"name": "USD Coin (PoS)",
"symbol": "USDC",
"display_symbol": None,
"optimized_symbol": "USDC",
"decimals": 6,
"logo_url": "https://static.debank.com/image/matic_token/logo_url/0x2791bca1f2de4661ed88a30c99a7a9449aa84174/fffcd27b9efff5a86ab942084c05924d.png",
"protocol_id": "",
"price": 1,
"is_verified": True,
"is_core": True,
"is_wallet": True,
"time_at": 1601199611,
"amount": 0.000054
},
{
"id": "0x831753dd7087cac61ab5644b308642cc1c33dc13",
"chain": "matic",
"name": "Quickswap",
"symbol": "QUICK",
"display_symbol": None,
"optimized_symbol": "QUICK",
"decimals": 18,
"logo_url": "https://static.debank.com/image/matic_token/logo_url/0x831753dd7087cac61ab5644b308642cc1c33dc13/3f40a8915d99b9dd0e24d5205c89eb34.png",
"protocol_id": "matic_quickswap",
"price": 165.3,
"is_verified": True,
"is_core": True,
"is_wallet": True,
"time_at": 1602175296,
"amount": 0.000194122619712451
}
],
"health_rate": 322.6968922103766
},
"proxy_detail": {}
}
]
}
]
我正在使用的 python 代码:
for i in data["portfolio_item_list"]["stats"]:
print(i["asset_usd_value"], i["debt_usd_value"], i["net_usd_value"])
我正在尝试保存用户投资组合数据。 我尝试在互联网上搜索可能的解决方案,但我对这些复杂的 JSON 结构非常陌生,并且找不到它的大量数据。我希望有人能帮助我解决我在这里做错的事情。
I tried accessing complex JSON data structures but i get this error:
TypeError: list indices must be integers or slices, not str
This is the JSON data response im working with:
data = [
{
"id": "matic_atlantis",
"chain": "matic",
"name": "Atlantis",
"site_url": "https://atlantis.loans",
"logo_url": "https://static.debank.com/image/project/logo_url/matic_atlantis/a1334c12971c12f4796ea439fbc1c5f8.png",
"has_supported_portfolio": True,
"tvl": 0,
"portfolio_item_list": [
{
"stats": {
"asset_usd_value": 20.258310032314448,
"debt_usd_value": 0.03452806117121324,
"net_usd_value": 20.223781971143236
},
"update_at": 1645959230.678967,
"name": "Lending",
"pool_id": "0x8f85ee1c0a96734cb76870106dd9c016db6de09a",
"detail_types": [
"lending"
],
"detail": {
"supply_token_list": [
{
"id": "matic",
"chain": "matic",
"name": "MATIC",
"symbol": "MATIC",
"display_symbol": None,
"optimized_symbol": "MATIC",
"decimals": 18,
"logo_url": "https://static.debank.com/image/matic_token/logo_url/matic/e5a8a2860ba5cf740a474dcab796dc63.png",
"protocol_id": "",
"price": 1.525962,
"is_verified": True,
"is_core": True,
"is_wallet": True,
"time_at": None,
"amount": 0.00007212821116324932,
"is_collateral": True
},
{
"id": "0x831753dd7087cac61ab5644b308642cc1c33dc13",
"chain": "matic",
"name": "Quickswap",
"symbol": "QUICK",
"display_symbol": None,
"optimized_symbol": "QUICK",
"decimals": 18,
"logo_url": "https://static.debank.com/image/matic_token/logo_url/0x831753dd7087cac61ab5644b308642cc1c33dc13/3f40a8915d99b9dd0e24d5205c89eb34.png",
"protocol_id": "matic_quickswap",
"price": 165.3,
"is_verified": True,
"is_core": True,
"is_wallet": True,
"time_at": 1602175296,
"amount": 0.12255414378345482,
"is_collateral": True
}
],
"borrow_token_list": [
{
"id": "matic",
"chain": "matic",
"name": "MATIC",
"symbol": "MATIC",
"display_symbol": None,
"optimized_symbol": "MATIC",
"decimals": 18,
"logo_url": "https://static.debank.com/image/matic_token/logo_url/matic/e5a8a2860ba5cf740a474dcab796dc63.png",
"protocol_id": "",
"price": 1.525962,
"is_verified": True,
"is_core": True,
"is_wallet": True,
"time_at": None,
"amount": 0.000002603921715322
},
{
"id": "0x53e0bca35ec356bd5dddfebbd1fc0fd03fabad39",
"chain": "matic",
"name": "ChainLink Token",
"symbol": "LINK",
"display_symbol": None,
"optimized_symbol": "LINK",
"decimals": 18,
"logo_url": "https://static.debank.com/image/matic_token/logo_url/0x53e0bca35ec356bd5dddfebbd1fc0fd03fabad39/69425617db0ef93a7c21c4f9b81c7ca5.png",
"protocol_id": "",
"price": 14.5811,
"is_verified": True,
"is_core": True,
"is_wallet": True,
"time_at": 1598767775,
"amount": 0.000163336006690615
},
{
"id": "0x2791bca1f2de4661ed88a30c99a7a9449aa84174",
"chain": "matic",
"name": "USD Coin (PoS)",
"symbol": "USDC",
"display_symbol": None,
"optimized_symbol": "USDC",
"decimals": 6,
"logo_url": "https://static.debank.com/image/matic_token/logo_url/0x2791bca1f2de4661ed88a30c99a7a9449aa84174/fffcd27b9efff5a86ab942084c05924d.png",
"protocol_id": "",
"price": 1,
"is_verified": True,
"is_core": True,
"is_wallet": True,
"time_at": 1601199611,
"amount": 0.000054
},
{
"id": "0x831753dd7087cac61ab5644b308642cc1c33dc13",
"chain": "matic",
"name": "Quickswap",
"symbol": "QUICK",
"display_symbol": None,
"optimized_symbol": "QUICK",
"decimals": 18,
"logo_url": "https://static.debank.com/image/matic_token/logo_url/0x831753dd7087cac61ab5644b308642cc1c33dc13/3f40a8915d99b9dd0e24d5205c89eb34.png",
"protocol_id": "matic_quickswap",
"price": 165.3,
"is_verified": True,
"is_core": True,
"is_wallet": True,
"time_at": 1602175296,
"amount": 0.000194122619712451
}
],
"health_rate": 322.6968922103766
},
"proxy_detail": {}
}
]
}
]
The python code im using:
for i in data["portfolio_item_list"]["stats"]:
print(i["asset_usd_value"], i["debt_usd_value"], i["net_usd_value"])
I'm trying to save the users portfolio data.
I've tried to search the internet for possible solutations but i'm very new to these kind of complex JSON structures and cannot find alot of data of it. I hope someone can help me out what im doing wrong here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的问题发生是因为你试图访问数据变量,就像它是一个字典:
data["portfolio_item_list"]["stats"]
但它是一个列表。您还在
portfolio_item_list
上执行了此操作。试试这个:
在这里,您首先访问数据数组中的第一个元素,然后访问该元素的值。
您应该做的第二件事是将
stats
更改为如下列表:因为您不能像列表一样循环遍历字典。
Your problem happens because your are trying to access the data var like it's a dict in:
data["portfolio_item_list"]["stats"]
but it's a list.You also did that on the
portfolio_item_list
.Try this:
Here, you first access the first elemt in the data array and then access the element's values.
Second thing you should do is to change the
stats
to a list like this:Because you can't loop through a dict like it's a list.