使用python从嵌套json中提取

时间:2018-07-25 19:08:18

标签: python json

我正在尝试从json输出下面提取值,我正在使用python提取键和值,以实现我尝试使用下面的代码并且我能够获取该值,但是它也显示了以下错误

“ KeyError:'集群成员名称'”

是否有更好的方法可以实现此目标?

resp_dict2 = json.loads(r23con)
for each in resp_dict2['objects']:
  print each['cluster-member-names']

==============================

    {
  "objects" : [ {
    "uid" : "b369119d-e3e5-4d12-9bb5-11d37524cf59",
    "name" : "clus-cn-1",
    "type" : "CpmiGatewayCluster",
    "domain" : {
      "uid" : "c3ca57db-f2cb-4ec0-bea7-2dca87d30d77",
      "name" : "dom-cn-1",
      "domain-type" : "domain"
    },
    "policy" : {
      "access-policy-installed" : true,
      "access-policy-name" : "Standard",
      "access-policy-installation-date" : {
        "posix" : 1531837503075,
        "iso-8601" : "2018-07-17T19:55+0530"
      },
      "threat-policy-installed" : false
    },
    "operating-system" : "Gaia",
    "hardware" : "Open server",
    "version" : "R80.10",
    "ipv4-address" : "53.126.102.12",
    "network-security-blades" : {
      "firewall" : true
    },
    "management-blades" : { },
    "cluster-member-names" : [ "dev-cn-c1", "dev-cn-c2" ],
    "vpn-encryption-domain" : "addresses_behind_gw",
    "sic-status" : "uninitialized",
    "tags" : [ ],
    "icon" : "NetworkObjects/cluster",
    "groups" : [ ],
    "comments" : "",
    "color" : "black",
    "meta-info" : {
      "lock" : "unlocked",
      "validation-state" : "ok",
      "last-modify-time" : {
        "posix" : 1531837459783,
        "iso-8601" : "2018-07-17T19:54+0530"
      },
      "last-modifier" : "System",
      "creation-time" : {
        "posix" : 1531824670186,
        "iso-8601" : "2018-07-17T16:21+0530"
      },
      "creator" : "System"
    },
    "read-only" : true
  }, {
    "uid" : "e24905b9-1cc7-4e8b-af2f-60553bfc2071",
    "name" : "clus-cn-2",
    "type" : "CpmiGatewayCluster",
    "domain" : {
      "uid" : "45d0fa6f-d218-455b-8fd9-c0c563e0e942",
      "name" : "dom-cn-2",
      "domain-type" : "domain"
    },
    "policy" : {
      "access-policy-installed" : true,
      "access-policy-name" : "Standard",
      "access-policy-installation-date" : {
        "posix" : 1531837708659,
        "iso-8601" : "2018-07-17T19:58+0530"
      },
      "threat-policy-installed" : false
    },
    "operating-system" : "Gaia",
    "hardware" : "Open server",
    "version" : "R80.10",
    "ipv4-address" : "53.126.102.16",
    "network-security-blades" : {
      "firewall" : true
    },
    "management-blades" : { },
    "cluster-member-names" : [ "dev-cn-c3", "dev-cn-c4" ],
    "vpn-encryption-domain" : "addresses_behind_gw",
    "sic-status" : "uninitialized",
    "tags" : [ ],
    "icon" : "NetworkObjects/cluster",
    "groups" : [ ],
    "comments" : "",
    "color" : "black",
    "meta-info" : {
      "lock" : "unlocked",
      "validation-state" : "ok",
      "last-modify-time" : {
        "posix" : 1531837666757,
        "iso-8601" : "2018-07-17T19:57+0530"
      },
      "last-modifier" : "System",
      "creation-time" : {
        "posix" : 1531833469334,
        "iso-8601" : "2018-07-17T18:47+0530"
      },
      "creator" : "System"
    },
    "read-only" : true
  }, {
    "uid" : "cf129905-3382-43df-bd4f-e0420a2ae425",
    "name" : "dev-cn-c1",
    "type" : "CpmiClusterMember",
    "domain" : {
      "uid" : "c3ca57db-f2cb-4ec0-bea7-2dca87d30d77",
      "name" : "dom-cn-1",
      "domain-type" : "domain"
    },
    "policy" : { },
    "ipv4-address" : "53.126.102.9",
    "interfaces" : [ {
      "interface-name" : "eth0",
      "ipv4-address" : "53.126.102.9",
      "ipv4-network-mask" : "255.255.255.192",
      "ipv4-mask-length" : 26,
      "dynamic-ip" : false,
      "topology" : {
        "leads-to-internet" : true
      }
    }, {
      "interface-name" : "eth1",
      "ipv4-address" : "10.10.10.1",
      "ipv4-network-mask" : "255.255.255.240",
      "ipv4-mask-length" : 28,
      "dynamic-ip" : false,
      "topology" : {
        "leads-to-internet" : false,
        "ip-address-behind-this-interface" : "network defined by the interface ip and net mask",
        "leads-to-dmz" : false
      }
    } ],
    "sic-status" : "communicating",
    "tags" : [ ],
    "icon" : "NetworkObjects/Cluster_member",
    "groups" : [ ],
    "comments" : "",
    "color" : "black",
    "meta-info" : {
      "lock" : "unlocked",
      "validation-state" : "ok",
      "last-modify-time" : {
        "posix" : 1531837458178,
        "iso-8601" : "2018-07-17T19:54+0530"
      },
      "last-modifier" : "System",
      "creation-time" : {
        "posix" : 1531824670721,
        "iso-8601" : "2018-07-17T16:21+0530"
      },
      "creator" : "System"
    },
    "read-only" : true
  }, {
    "uid" : "d35c980f-ba0f-40af-8593-466ef4caf90c",
    "name" : "dev-cn-c2",
    "type" : "CpmiClusterMember",
    "domain" : {
      "uid" : "c3ca57db-f2cb-4ec0-bea7-2dca87d30d77",
      "name" : "dom-cn-1",
      "domain-type" : "domain"
    },
    "policy" : { },
    "ipv4-address" : "53.126.102.10",
    "interfaces" : [ {
      "interface-name" : "eth1",
      "ipv4-address" : "10.10.10.2",
      "ipv4-network-mask" : "255.255.255.240",
      "ipv4-mask-length" : 28,
      "dynamic-ip" : false,
      "topology" : {
        "leads-to-internet" : false,
        "ip-address-behind-this-interface" : "network defined by the interface ip and net mask",
        "leads-to-dmz" : false
      }
    }, {
      "interface-name" : "eth0",
      "ipv4-address" : "53.126.102.10",
      "ipv4-network-mask" : "255.255.255.192",
      "ipv4-mask-length" : 26,
      "dynamic-ip" : false,
      "topology" : {
        "leads-to-internet" : true
      }
    } ],
    "sic-status" : "communicating",
    "tags" : [ ],
    "icon" : "NetworkObjects/Cluster_member",
    "groups" : [ ],
    "comments" : "",
    "color" : "black",
    "meta-info" : {
      "lock" : "unlocked",
      "validation-state" : "ok",
      "last-modify-time" : {
        "posix" : 1531837458844,
        "iso-8601" : "2018-07-17T19:54+0530"
      },
      "last-modifier" : "System",
      "creation-time" : {
        "posix" : 1531824673679,
        "iso-8601" : "2018-07-17T16:21+0530"
      },
      "creator" : "System"
    },
    "read-only" : true
  }, {
    "uid" : "2a35580d-865b-491f-b5ac-6bff38bedc03",
    "name" : "dev-cn-c3",
    "type" : "CpmiClusterMember",
    "domain" : {
      "uid" : "45d0fa6f-d218-455b-8fd9-c0c563e0e942",
      "name" : "dom-cn-2",
      "domain-type" : "domain"
    },
    "policy" : { },
    "ipv4-address" : "53.126.102.13",
    "interfaces" : [ {
      "interface-name" : "eth1",
      "ipv4-address" : "10.10.10.21",
      "ipv4-network-mask" : "255.255.255.240",
      "ipv4-mask-length" : 28,
      "dynamic-ip" : false,
      "topology" : {
        "leads-to-internet" : false,
        "ip-address-behind-this-interface" : "network defined by the interface ip and net mask",
        "leads-to-dmz" : false
      }
    }, {
      "interface-name" : "eth0",
      "ipv4-address" : "53.126.102.13",
      "ipv4-network-mask" : "255.255.255.192",
      "ipv4-mask-length" : 26,
      "dynamic-ip" : false,
      "topology" : {
        "leads-to-internet" : true
      }
    } ],
    "sic-status" : "communicating",
    "tags" : [ ],
    "icon" : "NetworkObjects/Cluster_member",
    "groups" : [ ],
    "comments" : "",
    "color" : "black",
    "meta-info" : {
      "lock" : "unlocked",
      "validation-state" : "ok",
      "last-modify-time" : {
        "posix" : 1531837665452,
        "iso-8601" : "2018-07-17T19:57+0530"
      },
      "last-modifier" : "System",
      "creation-time" : {
        "posix" : 1531833469733,
        "iso-8601" : "2018-07-17T18:47+0530"
      },
      "creator" : "System"
    },
    "read-only" : true
  }, {
    "uid" : "97b381c4-1b7c-4cd8-958c-f7a062bb731f",
    "name" : "dev-cn-c4",
    "type" : "CpmiClusterMember",
    "domain" : {
      "uid" : "45d0fa6f-d218-455b-8fd9-c0c563e0e942",
      "name" : "dom-cn-2",
      "domain-type" : "domain"
    },
    "policy" : { },
    "ipv4-address" : "53.126.102.14",
    "interfaces" : [ {
      "interface-name" : "eth1",
      "ipv4-address" : "10.10.10.20",
      "ipv4-network-mask" : "255.255.255.240",
      "ipv4-mask-length" : 28,
      "dynamic-ip" : false,
      "topology" : {
        "leads-to-internet" : false,
        "ip-address-behind-this-interface" : "network defined by the interface ip and net mask",
        "leads-to-dmz" : false
      }
    }, {
      "interface-name" : "eth0",
      "ipv4-address" : "53.126.102.14",
      "ipv4-network-mask" : "255.255.255.192",
      "ipv4-mask-length" : 26,
      "dynamic-ip" : false,
      "topology" : {
        "leads-to-internet" : true
      }
    } ],
    "sic-status" : "communicating",
    "tags" : [ ],
    "icon" : "NetworkObjects/Cluster_member",
    "groups" : [ ],
    "comments" : "",
    "color" : "black",
    "meta-info" : {
      "lock" : "unlocked",
      "validation-state" : "ok",
      "last-modify-time" : {
        "posix" : 1531837666008,
        "iso-8601" : "2018-07-17T19:57+0530"
      },
      "last-modifier" : "System",
      "creation-time" : {
        "posix" : 1531833470854,
        "iso-8601" : "2018-07-17T18:47+0530"
      },
      "creator" : "System"
    },
    "read-only" : true
  }, {
    "uid" : "ee6bad53-697a-4250-bd31-8c86886b3a45",
    "name" : "dom_cn_1_Server",
    "type" : "CpmiHostCkp",
    "domain" : {
      "uid" : "c3ca57db-f2cb-4ec0-bea7-2dca87d30d77",
      "name" : "dom-cn-1",
      "domain-type" : "domain"
    },
    "policy" : { },
    "operating-system" : "Unknown OS",
    "hardware" : "Open server",
    "version" : "R80.10",
    "ipv4-address" : "53.126.102.11",
    "network-security-blades" : { },
    "management-blades" : {
      "network-policy-management" : true,
      "logging-and-status" : true
    },
    "sic-status" : "uninitialized",
    "tags" : [ ],
    "icon" : "NetworkObjects/CheckPoint/Hosts/xHost_CP",
    "groups" : [ ],
    "comments" : "Domain Management Server",
    "color" : "black",
    "meta-info" : {
      "lock" : "unlocked",
      "validation-state" : "ok",
      "last-modify-time" : {
        "posix" : 1531803602643,
        "iso-8601" : "2018-07-17T10:30+0530"
      },
      "last-modifier" : "System",
      "creation-time" : {
        "posix" : 1531803574293,
        "iso-8601" : "2018-07-17T10:29+0530"
      },
      "creator" : "System"
    },
    "read-only" : true
  }, {
    "uid" : "d9539c38-8a7d-4c65-8c8e-a11e855409e0",
    "name" : "dom_cn_2",
    "type" : "CpmiHostCkp",
    "domain" : {
      "uid" : "45d0fa6f-d218-455b-8fd9-c0c563e0e942",
      "name" : "dom-cn-2",
      "domain-type" : "domain"
    },
    "policy" : { },
    "operating-system" : "Unknown OS",
    "hardware" : "Open server",
    "version" : "R80.10",
    "ipv4-address" : "53.126.102.15",
    "network-security-blades" : { },
    "management-blades" : {
      "network-policy-management" : true,
      "logging-and-status" : true
    },
    "sic-status" : "uninitialized",
    "tags" : [ ],
    "icon" : "NetworkObjects/CheckPoint/Hosts/xHost_CP",
    "groups" : [ ],
    "comments" : "Domain Management Server",
    "color" : "black",
    "meta-info" : {
      "lock" : "unlocked",
      "validation-state" : "ok",
      "last-modify-time" : {
        "posix" : 1531803969445,
        "iso-8601" : "2018-07-17T10:36+0530"
      },
      "last-modifier" : "System",
      "creation-time" : {
        "posix" : 1531803941175,
        "iso-8601" : "2018-07-17T10:35+0530"
      },
      "creator" : "System"
    },
    "read-only" : true
  } ],
  "from" : 1,
  "to" : 8,
  "total" : 8
}

1 个答案:

答案 0 :(得分:0)

问题在于,并非objects中的每个Json对象都有一个cluster-member-names字段。因此,当遍历所有对象并尝试访问该字段时(不存在该字段时),它将引发该KeyError。

要解决此问题,您可以使用dict.get('field_name', default_value)函数,如果该字段不存在,它将允许您指定默认值。默认情况下,通常为None

使用dict.get的示例:

resp_dict2 = json.loads(r23con)
for each in resp_dict2['objects']:
    val = each.get('cluster-member-names')
    if val is not None:
        print(val)