Python 3.x 有没有办法从其他键中的键获取值?

Python 3.x 有没有办法从其他键中的键获取值?,python-3.x,discord.py,Python 3.x,Discord.py,(第一篇帖子,如果我做错了,很抱歉)所以我正在使用discord.py为我和我的朋友制作一个机器人(关于discord)(因为python是有史以来最简单的代码),我遇到了这个问题。我需要从其他键中的键获取值。我该怎么做 因此,我尝试将res改为res.text、res.json和res.content,我只能找到“数据”,但找不到我需要的“id”、“name”或“description” 导入不一致 从discord.ext.commands导入Bot 从discord.ext导入命令 导入请

(第一篇帖子,如果我做错了,很抱歉)所以我正在使用discord.py为我和我的朋友制作一个机器人(关于discord)(因为python是有史以来最简单的代码),我遇到了这个问题。我需要从其他键中的键获取值。我该怎么做

因此,我尝试将res改为res.text、res.json和res.content,我只能找到“数据”,但找不到我需要的“id”、“name”或“description”

导入不一致
从discord.ext.commands导入Bot
从discord.ext导入命令
导入请求,json
导入异步
Client=discord.Client()
client=commands.Bot(命令前缀='?')
@客户端事件
_ready()上的异步定义:
打印('已开始')
@client.command()
异步def findfriends(ctx,用户ID):
res=requests.get(“https://friends.roblox.com/v1/users/“+userid+”/friends”)
var=json.load(res.text)
def a(a):
ID=a['ID']
返回ID
def b(b):
Name=b['Name']
返回名称
def c(c):
description=c['description']
返回说明
data=var['data']#我可以让它工作
打印(数据)
#cv=数据['name']#但这不起作用
#id=a(var)#也不是这个
#name=b(var)#也不是这个
#desc=c(var)#也不是这个
#等待ctx.send(“\nID:+id+”\nName:+name+”\nDesc:+desc)#这只是发送消息
client.run(这里是BOT令牌)#是的,我确实添加了它,但只是因为这个问题,我删除了它
正如我在代码中所说,我只能让“数据”工作,而不是id、name或desc。对于id name和desc,它只会抛出一个错误

忽略命令findfriends中的异常:
回溯(最近一次呼叫最后一次):
文件“C:\Users\Calculator\PycharmProjects\ryhrthrthrerbrebnngfbfg\venv\lib\site packages\discord\ext\commands\core.py”,第79行,换行
ret=等待coro(*args,**kwargs)
findfriends中第277行的文件“C:/Users/Calculator/PycharmProjects/RyHrthRhrRebNfbNgfbfg/a.py”
id=a(var)#也不是这个
文件“C:/Users/Calculator/pycharm项目/ryhrthrthrerbrebnfbnfbfg/a.py”,第266行,在a中
ID=a['ID']
KeyError:'id'
上述异常是以下异常的直接原因:
回溯(最近一次呼叫最后一次):
文件“C:\Users\Calculator\PycharmProjects\ryhrthrthrerbrebnngfbfg\venv\lib\site packages\discord\ext\commands\bot.py”,第863行,在invoke中
等待ctx.command.invoke(ctx)
文件“C:\Users\Calculator\PycharmProjects\ryhrthrthrerbrebnngfbfg\venv\lib\site packages\discord\ext\commands\core.py”,第728行,在invoke中
等待注入(*ctx.args,**ctx.kwargs)
文件“C:\Users\Calculator\PycharmProjects\ryhrthrthrerbrebnngfbfg\venv\lib\site packages\discord\ext\commands\core.py”,第88行,包装
从exc引发CommandInvokeError(exc)
discord.ext.commands.errors.CommandInvokeError:命令引发异常:KeyError:“id”

忽略命令findfriends中的异常:
回溯(最近一次呼叫最后一次):
文件“C:\Users\Calculator\PycharmProjects\ryhrthrthrerbrebnngfbfg\venv\lib\site packages\discord\ext\commands\core.py”,第79行,换行
ret=等待coro(*args,**kwargs)
findfriends中的文件“C:/Users/Calculator/PycharmProjects/ryhrthrthrerbrebnfbnfbfg/a.py”,第274行
data=var['data']['id']#我可以让它工作
TypeError:列表索引必须是整数或片,而不是str
上述异常是以下异常的直接原因:
回溯(最近一次呼叫最后一次):
文件“C:\Users\Calculator\PycharmProjects\ryhrthrthrerbrebnngfbfg\venv\lib\site packages\discord\ext\commands\bot.py”,第863行,在invoke中
等待ctx.command.invoke(ctx)
文件“C:\Users\Calculator\PycharmProjects\ryhrthrthrerbrebnngfbfg\venv\lib\site packages\discord\ext\commands\core.py”,第728行,在invoke中
等待注入(*ctx.args,**ctx.kwargs)
文件“C:\Users\Calculator\PycharmProjects\ryhrthrthrerbrebnngfbfg\venv\lib\site packages\discord\ext\commands\core.py”,第88行,包装
从exc引发CommandInvokeError(exc)
discord.ext.commands.errors.CommandInvokeError:命令引发异常:TypeError:列表索引必须是整数或片,而不是str

https://friends.roblox.com/v1/users//friends
endpoint返回用户拥有的所有朋友的列表,其大小可以不同

使用
var=json.loads(res.text)
将响应文本加载到json对象中,该对象包含键
data
,您可以使用
data=var['data']
访问该键。新的
data
变量现在包含一个列表对象,这就是为什么
cv=data['name']
无法工作的原因,因为列表对象不使用字符串作为键,而是使用整数访问它们

您需要遍历列表以获取有关好友的所有信息。下面的代码遍历列表,提取列表中每个项目的信息,并在遍历所有项目后发送信息消息

import discord
from discord.ext.commands import Bot
from discord.ext import commands
import requests, json
import asyncio

client = commands.Bot(command_prefix='?')
@client.event
async def on_ready():
    print('started')

@client.command()
async def findfriends(ctx,userid):
    res = requests.get("https://friends.roblox.com/v1/users/"+userid+"/friends")
    var = json.loads(res.text)
    data = var['data']
    print(data)

    friends_msg = 'Friends information:'
    for friend in data:
        id = friend['id']
        name = friend['name']
        desc = friend['description']
        friends_msg = friends_msg + "\nID: " + id + "\nName: " + name + "\nDesc: " + desc

    await ctx.send(friends_msg)

client.run(BOT TOKEN HERE)

您得到的是
KeyError
TypeError
——您确定
var
中有字典吗?您能否显示
var
中的内容?