如何使用动态密钥名称解析Python中的json?

时间:2016-04-07 20:59:06

标签: python json

我想从下面的JSON中提取name。我遇到的问题是JSON中的主机名是动态的,所以如果有意义的话,我不知道如何在该层下挖掘。所以' ip-10-12-68-170.b2c.test.com'每个json块都有一个不同的ip。

{
    "host" : {
        "ip-10-12-68-170.b2c.test.com" : {
            "environment" : {
                "testing1" : {
                    "ip" : "ip-10-12-68-170",
                    "name" : "testing",
                    "env.root" : "/",
                    "host" : "ip-10-12-68-170.b2c.test.com",
                    "sin" : "sin.80",
                    "env.description" : "Content Author Preview"
                }
            }
        },
        "ip-10-12-108.27.b2c.test.com" : {
            "environment" : {
                    "esbqav" : {
                    "ip" : "ip-10-12-108.27",
                    "name" : "espv",
                    "env.root" : "/",
                    "host" : "ip-10-12-108.27.b2c.test.com",
                    "sin" : "sin.0",
                    "env.description" : "QA"
                }
            }
        }
    }
}

如何从此示例中抓取name

2 个答案:

答案 0 :(得分:1)

可以使用字典values()items()方法,因为结构如示例所示。

import json

json_string = """
{
   "host" : {
      "ip-10-12-68-170.b2c.test.com" : {
         "environment" : {
            "testing1" : {
               "ip" : "ip-10-12-68-170",
               "name" : "testing",
               "env.root" : "/",
               "host" : "ip-10-12-68-170.b2c.test.com",
               "sin" : "sin.80",
               "env.description" : "Content Author Preview"
            }
         }
      },
    "ip-10-12-108.27.b2c.test.com" : {
       "environment" : {
          "esbqav" : {
             "ip" : "ip-10-12-108.27",
             "name" : "espv",
             "env.root" : "/",
             "host" : "ip-10-12-108.27.b2c.test.com",
             "sin" : "sin.0",
             "env.description" : "QA"
          }
       }
    }
     }
}
"""

json_data = json.loads(json_string)

for host in json_data.values():
    for hostname in host.values():
        environment = hostname.get('environment')

        for env in environment.values():
            name = env.get('name')
            print name

答案 1 :(得分:0)

您可以通过调用dict成员来迭代.items()。这样,您就不需要事先知道密钥是什么。

json= {
   "host" : {
      "ip-10-12-68-170.b2c.test.com" : {
         "environment" : {
            "testing1" : {
               "ip" : "ip-10-12-68-170",
               "name" : "testing",
               "env.root" : "/",
               "host" : "ip-10-12-68-170.b2c.test.com",
               "sin" : "sin.80",
               "env.description" : "Content Author Preview"
            }
         }
      },
      "ip-10-12-108.27.b2c.test.com" : {
         "environment" : {
            "esbqav" : {
               "ip" : "ip-10-12-108.27",
               "name" : "espv",
               "env.root" : "/",
               "host" : "ip-10-12-108.27.b2c.test.com",
               "sin" : "sin.0",
               "env.description" : "QA"
            }
         }
      }
   }
}
for ip, ip_dict in json['host'].items():
    for hostname, hostname_dict in ip_dict['environment'].items():
        name = hostname_dict['name']
        print (ip, hostname, name)

以下代码是等效的,但只迭代键而不是键值对:

for ip in json['host']:
    for hostname in json['host'][ip]['environment']:
        name = json['host'][ip]['environment'][hostname]['name']
        print (ip, hostname, name)