用 Python 解析 json GitHub api

Parsing json GitHub api with Python

难以从 GitHub api 解析 json。我正在尝试用一个组织的所有回购来填充一个团队。我正在使用 myteamname 获取循环所需的 teamid,该循环使用回购名称填充团队。

import json
import requests

mytokenid = "xxx"
myorg = "xxx"
myteamname = "xxx"


headers = {
    'Authorization': 'token %s' % mytokenid,
}

response = requests.get('https://api.github.com/orgs/{0}/teams/{1}'.format(myorg, myteamname), headers=headers)
teamidjson = json.loads(response.text)
myteamid = teamidjson(['id'])

g = Github(tokenid)
for repo in g.get_organization(myorg).get_repos():
    myreponame = repo.name
    response = requests.put('https://api.github.com/teams/{0}/repos/{1}/{2}'.format(myteamid, myorg, myreponame), headers=headers)

我收到此错误消息

  File "githubteam.py", line 74, in <module>
    myteamid = teamidjson(['id'])
TypeError: 'dict' object is not callable

myteamid = teamidjson(['id'])

这似乎是导致错误的原因。访问 id 键的正确方法是:

myteamid = teamidjson['id']

我想你的意思是

myteamid = teamidjson['id']

# you can also use this
myteamid = teamidjson.get('id', None) # it will default to None if id doesn't exist...