如何解析嵌套的JSON嵌套?

时间:2016-01-23 00:49:29

标签: python json dictionary

我在工作中使用此API时遇到困难,并且由于其复杂性而难以解析数据集。

在下面的JSON中,对我来说唯一重要的值是“name”和实际的主机名。我正在尝试创建一个由{“name”:“host,host,host,host”}组成的字典。如果有人知道如何解析这个或者能指出我正确的方向,那将非常感激。

{

    "hugeData":[
    {

        "env1":[
        {

            "sins":[
            {"host": "ip-10-12-138-225.va1.b2c.test.com", "deployTime": "2015-07-23 11:54 AM", "sin": "0"},
            {"host": "ip-10-12-129-193.va1.b2c.test.com", "deployTime": "2015-09-01 01:09 PM", "sin": "7"},
            {"host": "ip-10-12-138-235.va1.b2c.test.com", "deployTime": "2015-07-23 11:54 AM", "sin": "9"},
            {"host": "ip-10-12-138-250.va1.b2c.test.com", "deployTime": "2015-07-23 11:53 AM", "sin": "12"},
            {"host": "ip-10-12-138-223.va1.b2c.test.com", "deployTime": "2015-07-23 11:53 AM", "sin": "14"},
            {"host": "ip-10-12-138-237.va1.b2c.test.com", "deployTime": "2015-07-23 11:54 AM", "sin": "17"},
            {"host": "ip-10-12-138-244.va1.b2c.test.com", "deployTime": "2015-07-23 11:53 AM", "sin": "18"},
            ],

            "status": "success",

            "buildTime": "2015-05-26T17:06:06",

        }
        ],

        "name": "apache-6"
        },

    {
        "env1":[
        {
            "sins":[
            {"host": "ip-10-12-138-225.va1.b2c.test.com", "deployTime": "2015-12-16 05:23 PM", "sin": "0"},
            {"host": "ip-10-12-129-193.va1.b2c.test.com", "deployTime": "2015-12-16 05:23 PM", "sin": "7"},
            {"host": "ip-10-12-138-235.va1.b2c.test.com", "deployTime": "2015-12-16 05:23 PM", "sin": "9"},
            {"host": "ip-10-12-138-250.va1.b2c.test.com", "deployTime": "2015-12-16 05:23 PM", "sin": "12"},
            {"host": "ip-10-12-138-223.va1.b2c.test.com", "deployTime": "2015-12-16 05:23 PM", "sin": "14"},
            {"host": "ip-10-12-138-237.va1.b2c.test.com", "deployTime": "2015-12-16 05:23 PM", "sin": "17"},
            {"host": "ip-10-12-138-244.va1.b2c.test.com", "deployTime": "2015-12-16 05:23 PM", "sin": "18"},
            {"host": "ip-10-12-138-248.va1.b2c.test.com", "deployTime": "2015-12-16 05:23 PM", "sin": "21"},
            ],

            "status": "success",

            "buildTime": "2015-12-16T17:07:44",
        }
        ],

        "name": "apache-5"
        },

    {
        "env1":[
        {
            "sins":[
            {"host": "ip-10-12-138-234.va1.b2c.test.com", "deployTime": "2015-08-06 03:13 PM", "sin": "10"},
            {"host": "ip-10-12-138-246.va1.b2c.test.com", "deployTime": "2015-08-06 03:15 PM", "sin": "20"},
            {"host": "ip-10-12-138-216.va1.b2c.test.com", "deployTime": "2015-08-06 03:04 PM", "sin": "28"}
            ],

            "status": "success",

            "buildTime": "2013-02-21T15:41:59",

        }
        ],

        "name": "app-steel"
        },
}

1 个答案:

答案 0 :(得分:0)

这次也许你喜欢这个答案。

(将dict / json指定给名为test的varibale)

your_dict = {i['name']: ", ".join([host['host'] for host in i['env1'][0]['sins']]) \
              for i in test["hugeData"]}