使用Python解析JSON嵌套字典 [英] Parsing JSON nested Dictionary using Python

查看:127
本文介绍了使用Python解析JSON嵌套字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JSON中具有以下嵌套词典.如果要获取"id","self","name",应该如何使用Python程序进行解析.

I have the following nested Dictionary in JSON. If I want to get "id", "self", "name", how should I parse it using a Python Program.

{
  "items": [
    {
      "id": "12345",
      "links": {
        "self": "https://www.google.com"
      },
      "name": "beast",
      "type": "Device"
    }
  ],
  "links": {
    "self": "https://www.google.com"
  },
  "paging": {
    "count": 1,
    "limit": 1,
    "offset": 0,
    "pages": 1
  }
}

推荐答案

要了解json的设置方式,将其分解会更容易.让我们看一下第一个字典的键,然后删除值.

To understand how your json is set up, it's easier to break it down. Let's look at the first dictionary's keys, and remove the values.

json = {"items":[],"links":{}}

您有一本具有两个键和两个值的字典.您要查找的所有三个变量(id,self,name)都在第一个键"items"中.因此,让我们进一步深入.

You have a dictionary with two keys and two values. All three of the variables you are looking for (id, self, name) are in the first key, "items". So let's dive deeper.

json ["items"] = [{'links':{'self':'https://www.google.com'},'name':'beast','type':'设备","id":"12345"}]

现在您有了一个包含要查找的值的字典的列表,所以让我们输入包含下一个字典的列表的第一个也是唯一的值.

Now you have a list containing a dictionary with the values you are looking for, so let's enter the first and only value of the list containing the next dictionary.

json ["items"] [0] = {'links':{'self':'https://www.google.com'},'id':'12345','type':'设备','名称':'野兽'}

最后,我们有了要查找其值的字典,因此您可以使用此代码查找名称和ID.

Finally we have the dictionary with the values are looking for, so you can use this code to find name and id.

json ["items"] [0] ["name"] =野兽

json ["items"] [0] ["id"] = 12345

自变量在一个字典中更深地被隐藏,因此我们必须深入研究链接键.

The self variable is hidden one dictionary deeper so we have to delve into the links key.

json ["items"] [0] ["links"] ["self"] = http://google.com

现在您拥有了所有的价值,您只需要遵循所有列表和字典来获取所需的价值即可.

Now you have all of your values, you just need to follow through all the lists and dictionaries to get the value you want.

这篇关于使用Python解析JSON嵌套字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆