【发布时间】: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