解析json文件时处理多个keyerror

时间:2017-03-23 14:41:20

标签: python json python-2.7 parsing

在从rest api解析json文件时,我遇到了KeyErrors的问题。我想知道是否有更好的方法来处理这些。

我有多个密钥,并且为每个人都变得笨拙而烦恼。

Python代码:

url = 'https://ciscoprime.com/webacs/api/v2/data/ClientDetails.json?.nocount=True&.maxResults=2&.firstResult=0&.full=true' % (username, password)
get_response = requests.get(url, verify=False)
response = json.dumps(get_response.json())
resp = json.loads(response)

#variable = resp['queryResponse']['entity'][i]['clientDetailsDTO']['']

i = 0
while i < len(resp['queryResponse']['entity']):
    try:
        clientID = resp['queryResponse']['entity'][i]['clientDetailsDTO']['@id']
        associationTime = resp['queryResponse']['entity'][i]['clientDetailsDTO']['associationTime']
        clientInterface = resp['queryResponse']['entity'][i]['clientDetailsDTO']['clientInterface']
        connectionType = resp['queryResponse']['entity'][i]['clientDetailsDTO']['connectionType']
        deviceIpAddress = resp['queryResponse']['entity'][i]['clientDetailsDTO']['deviceIpAddress']
        deviceName = resp['queryResponse']['entity'][i]['clientDetailsDTO']['deviceName']
        firstSeenTime = resp['queryResponse']['entity'][i]['clientDetailsDTO']['firstSeenTime']
        ifIndex = resp['queryResponse']['entity'][i]['clientDetailsDTO']['ifIndex']
        ipAddress = resp['queryResponse']['entity'][i]['clientDetailsDTO']['ipAddress']
        macAddress = resp['queryResponse']['entity'][i]['clientDetailsDTO']['macAddress']
        updateTime = resp['queryResponse']['entity'][i]['clientDetailsDTO']['updateTime']
        vendor = resp['queryResponse']['entity'][i]['clientDetailsDTO']['vendor']
        vlan = resp['queryResponse']['entity'][i]['clientDetailsDTO']['vlan']
        vlanName = resp['queryResponse']['entity'][i]['clientDetailsDTO']['vlanName']
        #print 'ClientID: {0}, associationTime: {1}'.format(clientID, associationTime)
        i += 1
    except KeyError:
        print 'KeyError'

来自GET的示例输入:

{
  "queryResponse": {
    "@last": 2,
    "@first": 0,
    "@count": 191878,
    "@type": "ClientDetails",
    "@requestUrl": "https://ciscoprime.com/webacs/api/v2/data/ClientDetails?.full=true&amp;.maxResults=3",
    "@responseType": "listEntityInstances",
    "@rootUrl": "https://ciscoprime.com/webacs/api/v2/data",
    "entity": [
      {
        "@dtoType": "clientDetailsDTO",
        "@type": "ClientDetails",
        "@url": "https://ciscoprime.com/webacs/api/v2/data/ClientDetails/4110106",
        "clientDetailsDTO": {
          "@displayName": "46",
          "@id": 46,
          "apIpAddress": {
            "address": "127.0.0.1"
          },
          "apMacAddress": "FF:FF:FF:FF:FF:FF",
          "apName": "ooff-ap2",
          "apSlotId": 0,
          "associationTime": 1488178285705,
          "authenticationAlgorithm": "OPENSYSTEM",
          "ccxFSVersion": "V1",
          "ccxLSVersion": "V1",
          "ccxMSVersion": "V1",
          "ccxVSVersion": "V1",
          "ccxVersion": "UNSUPPORTED",
          "clientAaaOverrideAclApplied": "NA",
          "clientAclApplied": "NA",
          "clientApMode": "UNKNOWN",
          "clientInterface": "management",
          "connectionType": "LIGHTWEIGHTWIRELESS",
          "deviceIpAddress": {
            "address": "172.1.1.1"
          },
          "deviceName": "cntl-1",
          "deviceType": "none",
          "eapType": "UNNOWN",
          "encryptionCypher": "NONE",
          "firstSeenTime": 1487771895967,
          "hreapLocallyAuthenticated": "2",
          "ifIndex": 0,
          "ipAddress": {
            "address": "10.0.0.0"
          },
          "ipType": "IPV4",
          "location": "Root Area",
          "macAddress": "FF:FF:FF:FF:FF:FF",
          "mobilityStatus": "EXPORTANCHORED",
          "nacState": "ACCESS",
          "policyType": "NA",
          "policyTypeStatus": "NOMETHOD",
          "postureStatus": "UNKNOWN",
          "protocol": "DOT11N2_4GNZ",
          "securityPolicyStatus": "FAILED",
          "speed": "UNKNOWN",
          "ssid": "Guest",
          "status": "ASSOCIATED",
          "updateTime": 1490275781871,
          "vendor": "Samsung Electronics",
          "vlan": "10",
          "webSecurity": "DISABLED",
          "wepState": "DISABLED",
          "wgbMacAddress": "00:00:00:00:00:00",
          "wgbStatus": "REGULARCLIENT",
          "wiredClientType": "NA"
        }
      }

在这种情况下,vlanName不存在导致KeyError。但是,当它可以是多个键时会发生什么?有关处理这些错误的最佳方法的任何帮助都会很棒。

0 个答案:

没有答案