【问题标题】:Parsing JSON using Python?使用 Python 解析 JSON?
【发布时间】:2015-04-16 01:18:53
【问题描述】:

我正在使用 GitHub API 来提取关于存储库提交的 JSON 数据。

指向 JSON 文件的链接:https://api.github.com/repos/ErinBailey/cs399-social/commits

我正在尝试获取所有致力于项目的用户的“ID”。我该怎么做呢?现在我看到 JSON 文件的结构是 sha/author/id。

到目前为止,我的代码是:

r = requests.get('https://api.github.com/repos/ErinBailey/cs399-social/commits', auth=('cs399contributions', 'contributions399'))

input_log = json.loads(r.text)
print(json.dumps(input_log, indent=4))
user_ids = [x for x in input_log if x['sha/commit/author/id'] > '0']
output_json = json.dumps(user_ids,indent=4)
print output_json

【问题讨论】:

    标签: python json git


    【解决方案1】:

    这是我的代码。

    import requests
    import json
    
    r = requests.get('https://api.github.com/repos/ErinBailey/cs399-social/commits',
                     auth=('cs399contributions', 'contributions399'))
    
    input_log = json.loads(r.text)
    user_ids = [x['committer']['id'] for x in input_log if x['committer'].get('id')]
    output_json = json.dumps(user_ids, indent=4)
    print output_json
    

    【讨论】:

    • 我收到以下错误:TypeError: string indices must be integers on user_ids = [x['committer']['id'] for x in input_log if x['committer'].get( 'id')]
    • 这与我遍历字典而不是列表有关吗?
    猜你喜欢
    • 2018-08-12
    • 1970-01-01
    • 1970-01-01
    • 2019-08-11
    • 1970-01-01
    • 2023-04-02
    • 2020-09-20
    • 2016-07-17
    相关资源
    最近更新 更多