【问题标题】:Parsing json GitHub api with Python用 Python 解析 json GitHub api
【发布时间】:2020-09-08 10:51:03
【问题描述】:

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

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

【问题讨论】:

  • 你有什么问题?
  • 只有当我对 teamid 进行硬编码时,带有 for 循环的后半部分才有效。我想要使​​用的是从团队名称中提取 teamid 并使用它。我该怎么做?
  • 您是否要将其添加到问题本身?您还应该从响应中添加 JSON。

标签: python json github github-api


【解决方案1】:

myteamid = teamidjson(['id'])

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

myteamid = teamidjson['id']

【讨论】:

    【解决方案2】:

    我想你的意思是

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-06
      • 1970-01-01
      • 2019-12-30
      • 1970-01-01
      • 2018-07-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多