从API中访问列表中的列表内的字典数据。

huangapple go评论60阅读模式
英文:

Access data from a dictionary within a list within a list from api

问题

{
"get": "standings",
"parameters": {
"league": "12",
"season": "2019-2020"
},
"errors": [],
"results": 1,
"response": [
[
{
"position": 1,
"stage": "NBA - Regular Season",
"group": {
"name": "Western Conference",
"points": null
},
...
}
]
]
}

英文:
{
"get": "standings",
"parameters": {
    "league": "12",
    "season": "2019-2020"
},
"errors": [],
"results": 1,
"response": [
    [
        {
            "position": 1,
            "stage": "NBA - Regular Season",
            "group": {
                "name": "Western Conference",
                "points": null
            },

I am being returned information from an API that I would like to access in Python. I would like to access the "position" element. I have tried the following code to no avail:

"position": response['response'][0]['id'],

This seems to give me either a key Error or an Attribute Error. Can anyone give me some help with this?

答案1

得分: 1

你应该这样做:

position = response["response"][0][0]["position"]
print(position) # 1
英文:

You should do this instead:

position = response["response"][0][0]["position"]
print(position) # 1

huangapple
  • 本文由 发表于 2023年2月9日 00:59:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389186.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定