首页 > 解决方案 > 在python中读取json中的数组

问题描述

我正在尝试在 json 中的数组中读取值(dload 是一个从 url 读取 json 的包,我想访问“用户名”:“Mic123”)

json

{"id": "884087991803124224", "name": "Test's server", "instant_invite": null, "channels": [{"id": "884087923803124228", "name": "General", "position": 0}], "members": [{"id": "0", "username": "Mic123", "discriminator": "0000", "avatar": null, "status": "online", "avatar_url": "https://cdn.discordapp.com/widget-avatars/-WkB9BvIc5Cf98sDorItQJAxQe2zFRSPu2rUMZa329E/ax2jbnjNHRatHiSN0ErxEAFWlGKIAriBh4nOAWpetSDxpYAZRM7a4vGODKipMekSOnHqEdvti4MFVMsYXPnK7gmYU6yowd8FhxSqq5dKIH2a3ouw7eW9uisKiCogMiBpbwOfM4K_GuZX5Q"}], "presence_count": 1}

我试试这段代码

print(dload.json(licencekeyurl['members'][0]["username"]))

输出 TypeError: string indices must be integers

标签: pythonjson

解决方案


for key,value in data.items():
if key == "members":
    for key1,value1 in value[0].items():
        print(value1)

推荐阅读