Discord.py bot给出错误:TypeError:列表索引必须是整数或切片,而不是str

时间:2018-07-19 22:36:11

标签: json python-3.x discord.py

所以,我有这个正在操作存款功能的Tipbot。

我有此代码,我正在研究:

d2['data']['balances']['address']
embed=discord.Embed(description="{}".format(d2['data']['balances']['address']), color=0x00ff00)
await client.say(embed=embed)

但是,在解决此问题之后,当我尝试从bot调用函数时,出现以下错误:

TypeError: list indices must be integers or slices, not str

我需要怎么做才能修复以上代码? (我正在编写第一行代码。)

我提取的json片段是:

{'status': 'success', 'data': {'network': 'DOGE', 'available_balance': '0.0', 'pending_received_balance': '0.0', 'balances': [{'user_id': 1, 'label': 'shibe1', 'address': 'A9Bda9UMBcb1183PtsBxnbj5QgP6jwkCFG', 'available_balance': '0.00000000', 'pending_received_balance': '0.00000000'}]}}

1 个答案:

答案 0 :(得分:0)

您需要考虑以下事实:您的JSON返回一个地址列表,在这种情况下,这意味着您要查找的内容实际上位于d2['data']['balances'][0]['address']而非d2['data']['balances']['address']