在 Python 字典中处理来自 REST API 的多维响应对象

Handling a multidimensional response object from REST API in Python dictionary

我正在尝试处理从 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())

而且钥匙完美地回来了。但是,在特定键内,会返回一个列表。我对该列表中包含的值感兴趣。具体来说,我关心 'values' 内的值。有什么方法可以解析出值中包含的信息before/after is becomes a list?

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

{
  "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
}

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

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

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

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

这是否回答了问题?