用Python解析JSON以获得特定的值

2024-06-17 10:40:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用Python解析JSON。我试图得到“login”的值,它是michael的值,而“type”是CreateEvent的值。你知道吗

这是我的JSON:

[
  {
    "id": "7",
    "type": "PushEvent",
    "actor": {
      "id": 5,
      "login": "michael",
      "display_login": "michael",
      "gravatar_id": "",
      "url": "https://ec2",
      "avatar_url": "https://ec2"
    },
    "repo": {
      "id": 2,
      "name": "myorganization/puppet",
      "url": "https://ec2"
    },
    "payload": {
      "push_id": 5,
      "size": 1,
      "distinct_size": 1,
      "ref": "refs/heads/dev",
      "head": "5584d504f971",
      "before": "e485f37ce935775846f33b",
      "commits": [
        {
          "sha": "5584cd504f971",
          "author": {
            "email": "michael.conte@gmail.ca",
            "name": "michael"
          },
          "message": "Create dev.pp",
          "distinct": true,
          "url": "https://ec2"
            }
          ]
        },
        "public": true,
    "created_at": "2018-02-20T16:15:57Z",
    "org": {
      "id": 6,
      "login": "myorganization",
      "gravatar_id": "",
      "url": "https://ec2",
      "avatar_url": "https://ec2"
    }
  },
      {
    "id": "6",
    "type": "CreateEvent",
    "actor": {
      "id": 5,
      "login": "michael",
      "display_login": "michael",
      "gravatar_id": "",
      "url": "https://ec2",
      "avatar_url": "https://ec2"
    },
    "repo": {
      "id": 2,
      "name": "myorganization/puppet",
      "url": "https://ec2"
    },
    "payload": {
      "ref": "dev",
      "ref_type": "branch",
      "master_branch": "master",
      "description": null,
      "pusher_type": "user"
    },
    "public": true,
    "created_at": "2018-02-20T16:15:44Z",
    "org": {
      "id": 6,
      "login": "myorganization",
      "gravatar_id": "",
      "url": "https://ec2",
  "avatar_url": "https://ec2"
    }
  },
  {
    "id": "5",
    "type": "PushEvent",
    "actor": {
      "id": 5,
      "login": "michael",
      "display_login": "michael",
      "gravatar_id": "",
      "url": "https://ec2",
      "avatar_url": "https://ec2"
    },
    "repo": {
      "id": 2,
      "name": "myorganization/puppet",
      "url": "https://ec2"
        },
        "payload": {
          "push_id": 3,
      "size": 1,
      "distinct_size": 1,
      "ref": "refs/heads/master",
  "head": "e485f84b875846f33b",
  "before": "f8bb87b952bfb4",
  "commits": [
    {
      "sha": "e485f37ce6f33b",
      "author": {
        "email": "michael.conte@gmail.ca",
        "name": "michael"
      },
      "message": "Create hello.pp",
      "distinct": true,
      "url": "https://ec2"
    }
  ]
    },
    "public": true,
    "created_at": "2018-02-20T15:48:42Z",
    "org": {
      "id": 6,
      "login": "myorganization",
      "gravatar_id": "",
      "url": "https://ec2",
      "avatar_url": "https://ec2"
    }
  },
  {
    "id": "4",
    "type": "CreateEvent",
    "actor": {
      "id": 5,
      "login": "michael",
      "display_login": "michael",
      "gravatar_id": "",
      "url": "https://ec2",
      "avatar_url": "https://ec2?"
    },
    "repo": {
      "id": 2,
      "name": "myorganization/puppet",
      "url": "https://ec2"
    },
    "payload": {
      "ref": "master",
      "ref_type": "branch",
      "master_branch": "master",
      "description": null,
      "pusher_type": "user"
    },
    "public": true,
    "created_at": "2018-02-20T15:48:21Z",
    "org": {
      "id": 6,
      "login": "myorganization",
      "gravatar_id": "",
      "url": "https://ec2",
      "avatar_url": "https://ec2"
    }
  },
  {
    "id": "3",
    "type": "CreateEvent",
    "actor": {
      "id": 5,
      "login": "michael",
      "display_login": "michael",
      "gravatar_id": "",
      "url": "https://ec2",
      "avatar_url": "https://ec2"
    },
    "repo": {
      "id": 2,
  "name": "myorganization/puppet",
  "url": "https://ec2"
      },
     "payload": {
        "ref": null,
        "ref_type": "repository",
       "master_branch": "master",
        "description": null,
        "pusher_type": "user"
      },
      "public": true,
      "created_at": "2018-02-20T15:48:05Z",
      "org": {
        "id": 6,
        "login": "myorganization",
        "gravatar_id": "",
        "url": "https://ec2",
        "avatar_url": "https://ec2"
      }
        }
      ]

这是我的密码:

response = requests.get(url, headers=headers, verify=False)
name = response.json()
fname = (name['type']['actor']['login'])
print(fname)

当我运行上面的代码时,我得到一个类型错误。你知道吗

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

我做错什么了?我正在使用Python3作为我的代码。你知道吗


Tags: namehttpsmasterrefidtrueurltype
1条回答
网友
1楼 · 发布于 2024-06-17 10:40:29

试试看

fname = name[0]['payload']['commits'][0]['author']['name']

您试图获取的名称Michael位于名为author的字典中,该字典位于单个项列表中,该字典位于提交字典中,该字典位于有效负载字典中,该字典位于单个项列表中。你知道吗

有关集合类型的详细信息,请查看文档:http://python-textbok.readthedocs.io/en/1.0/Collections.html

相关问题 更多 >