【问题标题】:Using requests.json() returns a list, not JSON使用 requests.json() 返回一个列表,而不是 JSON
【发布时间】:2021-07-22 14:28:50
【问题描述】:

我正在尝试从 GitHub 拉取请求中获取 tag_name,但无论我做什么,我总是得到一个列表而不是 JSON。我希望能够将tag_name 分开并将其用于其他用途。

代码:

json_file = requests.get(url = "https://api.github.com/repos/USERNAME/REPONAME/releases", auth=("USERNAME","TOKEN")).json()

它总是返回一个列表。

"[{\"url\":\"https://api.github.com/repos/USERNAME/REPONAME/releases/42178803\",\"assets_url\":\"https://api.github.com/repos/USERNAME/REPONAME/releases/42178803/assets\",\"upload_url\":\"https://uploads.github.com/repos/USERNAME/REPONAME/releases/42178803/assets{?name,label}\",\"html_url\":\"https://github.com/USERNAME/REPONAME/releases/tag/v1.0-pre\",\"id\":42178803,\"author\":{\"login\":\"USERNAME\",\"id\":72929861,\"node_id\":\"MDQ6VXNlcjcyOTI5ODYx\",\"avatar_url\":\"https://avatars.githubusercontent.com/u/72929861?v=4\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/USERNAME\",\"html_url\":\"https://github.com/USERNAME\",\"followers_url\":\"https://api.github.com/users/USERNAME/followers\",\"following_url\":\"https://api.github.com/users/USERNAME/following{/other_
[{'url': 'https://api.github.com/repos/USERNAME/REPONAME/releases/42178803', 'assets_url': 'https://api.github.com/repos/USERNAME/REPONAME/releases/42178803/assets', 'upload_url': 'https://uploads.github.com/repos/USERNAME/REPONAME/releases/42178803/assets{?name,label}', 'html_url': 'https://github.com/USERNAME/REPONAME/releases/tag/v1.0-pre', 'id': 42178803, 'author': {'login': 'USERNAME', 'id': 72929861, 'node_id': 'MDQ6VXNlcjcyOTI5ODYx', 'avatar_url': 'https://avatars.githubusercontent.com/u/72929861?v=4', 'gravatar_id': '', 'url': 'https://api.github.com/users/USERNAME', 'html_url': 'https://github.com/USERNAME', 'followers_url': 'https://api.github.com/users/USERNAME/followers', 'following_url': 'https://api.github.com/users/USERNAME/following{/other_user}', 'gists_url': 'https://api.github.com/users/USERNAME/gists{/gist_id}', 'starred_url': 'https://api.github.com/users/USERNAME/starred{/owner}{/repo}', 'subscriptions_url': 'https://api.github.com/users/USERNAME/subscriptions', 'organizations_url': 'https://api.github.com/users/USERNAME/orgs', 'repos_url': 'https://api.github.com/users/USERNAME/repos', 'events_url': 'https://api.github.com/users/USERNAME/events{/privacy}', 'received_events_url': 'https://api.github.com/users/USERNAME/received_events', 'type': 'User', 'site_admin': False}, 'node_id': 'MDc6UmVsZWFzZTQyMTc4ODAz', 'tag_name': 'v1.0-pre', 'target_commitish': 'main', 'name': 'Prerelease', 'draft': False, 'prerelease': True, 'created_at': '2021-04-29T05:11:16Z', 'published_at': '2021-04-29T05:17:06Z', 'assets': [], 'tarball_url': 'https://api.github.com/repos/USERNAME/REPONAME/tarball/v1.0-pre', 'zipball_url': 'https://api.github.com/repos/USERNAME/REPONAME/zipball/v1.0-pre', 'body': 'Prerelease for a little test'}]

(刚刚做了一个打印(json_file.json()),这是结果)

我不知道发生了什么,请帮忙。

【问题讨论】:

  • 没错。 Json 是一个由列表、字典、字符串和数字组合而成的字符串。当您执行Response.json() 时,会将其转换为python 对象。通常 json 有一个 dict 作为顶层的东西(你的意思是它应该返回一个 json 吗?),但这里它是一个列表 - 所以你在转换时会得到一个列表。
  • @h4z3 是的。我习惯的 JSON 很可能是一个 dict,我可以在上面使用 print(json_file["tag_name"]) 的类型。

标签: python json github python-requests


【解决方案1】:

应该返回一个列表。毕竟,您从 releases 端点获得的信息,例如https://api.github.com/repos/python-babel/babel/releases,是一个列表。

import requests
resp = requests.get("https://api.github.com/repos/python-babel/babel/releases")
resp.raise_for_status()
releases_list = resp.json()
for release in releases_list:
    print(release["tag_name"])

打印出来

v2.9.1
v2.9.0
v2.8.1
v2.8.0
v2.7.0
v2.6.0
v2.5.3
v2.5.2
v2.5.1
v2.5.0
v2.4.0
2.3.4
2.3.2
2.3.1
2.2.0
2.1.1

【讨论】:

  • 那有没有办法只获取标签名呢?我想为 print(json_file["tag_name"]) 获取 JSON
  • 如果你想要最新版本的标签名称,print(releases_list[0]["tag_name"])
  • 来自 GitHub API 的 JSON 被解码为 Python 字典列表 - 索引 0 获取第一个版本(这是最新版本,因为 API 返回最新优先),然后您只需访问该字典像往常一样。
猜你喜欢
  • 2013-12-29
  • 1970-01-01
  • 1970-01-01
  • 2016-01-16
  • 1970-01-01
  • 2013-11-05
  • 2022-01-15
  • 2015-04-27
  • 1970-01-01
相关资源
最近更新 更多