0

我正在尝试处理从 Python 中的 BitBucket 的 API 返回的响应对象。我有以下代码来尝试处理响应:

import requests
import json

url = 'http:www.sampleurl.com'
myResponse = requests.get(url,auth=("myusername","mypassword"))

jd = myResponse.json()
print(jd.keys())

钥匙完美地回来了。但是,在特定键中,会返回一个列表。我对该列表中包含的值感兴趣。具体来说,我关心“价值观”中的价值观。有什么方法可以解析出列表之前/之后的值中包含的信息吗?

我的问题归结为在以下示例响应中访问 JSON 数组“父母”中的信息时遇到问题。

{
  "pagelen": 30,
  "values": [
    {
        hash: "61d9e64348f9da407e62f64726337fd3bb24b466",
        links: {
            self: {
                href: "https://api.bitbucket.org/2.0/repositories/atlassian/atlassian-rest/commit/61d9e64348f9da407e62f64726337fd3bb24b466"
            },
            comments: {
                href: "https://api.bitbucket.org/2.0/repositories/atlassian/atlassian-rest/commit/61d9e64348f9da407e62f64726337fd3bb24b466/comments"
            },
            patch: {
                href: "https://api.bitbucket.org/2.0/repositories/atlassian/atlassian-rest/patch/61d9e64348f9da407e62f64726337fd3bb24b466"
            },
            html: {
                href: "https://api.bitbucket.org/atlassian/atlassian-rest/commits/61d9e64348f9da407e62f64726337fd3bb24b466"
            },
            diff: {
                href: "https://api.bitbucket.org/2.0/repositories/atlassian/atlassian-rest/diff/61d9e64348f9da407e62f64726337fd3bb24b466"
            },
            approve: {
                href: "https://api.bitbucket.org/2.0/repositories/atlassian/atlassian-rest/commit/61d9e64348f9da407e62f64726337fd3bb24b466/approve"
            }
        },
        repository: {
            links: {
                self: {
                    href: "https://api.bitbucket.org/2.0/repositories/atlassian/atlassian-rest"
                },
                avatar: {
                    href: "https://d3oaxc4q5k2d6q.cloudfront.net/m/bf1e763db20f/img/language-avatars/java_16.png"
                }
            },
            full_name: "atlassian/atlassian-rest",
            name: "atlassian-rest"
        },
        author: {
            raw: "Joseph Walton <jwalton@atlassian.com>",
            user: {
                username: "jwalton",
                display_name: "Joseph Walton",
                links: {
                    self: {
                        href: "https://api.bitbucket.org/2.0/users/jwalton"
                    },
                    avatar: {
                        href: "https://secure.gravatar.com/avatar/8e6e91101e3ed8a332dbebfdf59a3cef?d=https%3A%2F%2Fd3oaxc4q5k2d6q.cloudfront.net%2Fm%2Fbf1e763db20f%2Fimg%2Fdefault_avatar%2F32%2Fuser_blue.png&s=32"
                    }
                }
            }
        },
        parents: [{
            hash: "59721f593b020123a75424285845325126f56e2e",
            links: {
                self: {
                    href: "https://api.bitbucket.org/2.0/repositories/atlassian/atlassian-rest/commit/59721f593b020123a75424285845325126f56e2e"
                }
            }
        }, {
            hash: "56c49d8b2ae3a094fa7ba5a1251d6dd2c7c66993",
            links: {
                self: {
                    href: "https://api.bitbucket.org/2.0/repositories/atlassian/atlassian-rest/commit/56c49d8b2ae3a094fa7ba5a1251d6dd2c7c66993"
                }
            }
        }],
        date: "2013-10-21T07:21:51+00:00",
        message: "Merge remote-tracking branch 'origin/rest-2.8.x' "
    }
  ],
  "page": 1
}
4

1 回答 1

1

不确定我是否正确理解了这个问题,但是您的 dict 缺少 jDo 提到的引号。假设这是一个格式良好的字典,您应该能够像这样访问父信息:

parents = jd["values"][0]["parents"]

然后你可以遍历父母列表并用它做任何你喜欢的事情:

for parent in parents:
    do_sth_with_parent(parent["hash"], parent["links"])

这能回答问题吗?

于 2016-03-25T00:38:30.990 回答