如何遍历嵌套字典?

时间:2018-06-07 15:24:15

标签: python json python-3.x

我正在学习python。我试图遍历从JSON

创建的嵌套字典

字典:

{
  "results": [
    {
      "id": "56898",
      "type": "page",
      "status": "current",
      "title": "The Desk",
      "macroRenderedOutput": {},
      "extensions": {
        "position": 0
      },
      "_expandable": {
        "childTypes": "",
        "container": "/rest/api/space/CSR",
        "metadata": "",
        "operations": "",
        "ancestors": "",
        "body": "",
        "version": "",
      },
      "_links": {
        "self": "https://mycontent.com/121212",
        "webui": "www.mycontent.com"
      }
    },

代码:

response = requests.get(url, headers=headers, auth=auth)
json_data = response.json()

for majorkey in json_data['results']:
    print(majorkey['title'])

这打印" The Desk"正如所料。我可以访问第一级的所有内容。

我尝试使用嵌套的for循环,可以访问子键但无法访问子键的值。

for majorkey in json_data['results']:
    print(majorkey['title'])
    for subkey in majorkey:
        print(subkey)

如何迭代"_links"以便我可以访问self并打印值https://mycontent.com/121212

0 个答案:

没有答案
相关问题