简体   繁体   English

尝试使用Python打印JSON数据

[英]Trying to print JSON data using Python

So I have a Discord BOT using python and I am trying to get print the JSON information using the Enjin API. 所以我有一个使用python的Discord BOT,我试图使用Enjin API打印JSON信息。 I can get it to print everything if I do print(Data) , but as soon as I try displaying a certain bit of information, such as the variables_values , it doesn't display and gives me the following error: 如果我print(Data) ,我可以打印所有内容,但是一旦我尝试显示某些信息,例如variables_values ,它就不会显示并给我以下错误:

Command raised an exception: TypeError: list indices must be integers or slices, not str 命令引发异常:TypeError:list indices必须是整数或切片,而不是str

I have a previous function; 我有一个以前的功能; called Status that uses a different API, and I can get it to work with that, but I just cannot get the following code to display the values in variables_values bracket. 调用Status使用不同的API,我可以使用它,但我无法获得以下代码来显示variables_values括号中的值。 (As you can tell, I am not great with JSON | Also, ignore the xxxxx ) (正如你所知,我对JSON不是很好|另外,忽略xxxxx

async def GetVariable(message):
    R = requests.get('http://www.xxxxxx.co.uk/api/m-shopping-purchases/m/47325895/user_id/19363576')
    Data = R.json()

    await client.say(Data["varaible_values"])

Here is what I want displayed, when I run the ?GetVariable command: jacobdallred - which is in the "variables_values":{"Steam64ID":"jacobdallred"} place. 这是我想要显示的内容,当我运行?GetVariable命令时: jacobdallred - 位于"variables_values":{"Steam64ID":"jacobdallred"}位置。

Instead, it gives me the error previously mentioned. 相反,它给了我前面提到的错误。

Here is what it displays if I only client.say(Data) : (Uploading to Hastebin to display nicer) https://hastebin.com/huwubiwawe.json 如果我只是client.say(Data) :(上传到Hastebin以显示更好),这是它显示的内容https://hastebin.com/huwubiwawe.json

You need to access the first element of that json response array. 您需要访问该json响应数组的第一个元素。 Like this: 像这样:

await client.say(Data[0]["items"][0]["variables_values"]["Steam64ID"])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM