How can I create a new JSON object form another using Python?
本问题已经有最佳答案,请猛点这里访问。
我需要使JSON对象的信息比原始对象少:
从api服务中获取原始json对象后,它看起来如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | { "queryResponse": { "@type":"AccessPointDetails", "@rootUrl":"https://xxx/webacs/api/v1/data", "@requestUrl":"https://xxx/webacs/api/v1/data/AccessPointDetails?.full=true&.firstResult=250&.maxResults=2", "@responseType":"listEntityInstances", "@count":"347", "@first":"250", "@last":"251", "entity": [ { "@url":"https://xxx/webacs/api/v1/data/AccessPointDetails/1897332", "@type":"AccessPointDetails", "@dtoType":"accessPointDetailsDTO", "accessPointDetailsDTO": { "@id":"1897332", "@displayName":"1897332", "adminStatus":"ENABLE", "apType":"AP1140", "clientCount": 1, "clientCount_2_4GHz": 1, "clientCount_5GHz": 0, "ethernetMac":"xxx", "ipAddress":"xxx", "locationHeirarchy":"Root Area", "macAddress":"xxx", "mapLocation":"xxx", "model":"AIR-LAP1141N-A-K9", "name":"xxx", "serialNumber":"xxx", "softwareVersion":"8.0.140.0", "status":"CLEARED", "type":"UnifiedAp", "unifiedApInfo": { "instanceId": 0, "instanceVersion": 0, "apCertType": 1, "apGroupName":"xxx", "apMode": 2, "apStaticEnabled": 0, "bootVersion":"12.4.23.6", "capwapJoinTakenTime": 1500, "capwapUpTime": 52444681, "controllerIpAddress":"xxx", "controllerName":"wlc_5508", "contryCode":"US", "encryptionEnabled": false, "flexConnectMode": false, "iosVersion":"15.3(3)JA10$", "linkLatencyEnabled": false, "poeStatus": 5, "portNumber": 13, "powerInjectorState": 1, "preStandardState": 0, "primaryMwar":"xxx", "rogueDetectionEnabled": true, "sshEnabled": false, "statisticsTimer": 180, "telnetEnabled": false, "vlanEnabled": true, "vlanNativeId": 16, "WIPSEnabled": 0, "wlanVlanMappings": { "wlanVlanMapping": { "ssid":"xxx", "vlanId": 220, "wlanId": 3 } } }, "upTime": 2666643681 } }, { "@url":"https://xxx/webacs/api/v1/data/AccessPointDetails/1897334", "@type":"AccessPointDetails", "@dtoType":"accessPointDetailsDTO", "accessPointDetailsDTO": { "@id":"1897334", "@displayName":"1897334", "adminStatus":"ENABLE", "apType":"AP3500E", "clientCount": 8, "clientCount_2_4GHz": 8, "clientCount_5GHz": 0, "ethernetMac":"xxx", "ipAddress":"xxx", "locationHeirarchy":"Root Area", "macAddress":"xxx", "mapLocation":"xxx", "model":"AIR-CAP3501E-A-K9", "name":"xxx", "serialNumber":"xxx", "softwareVersion":"8.1.131.0", "status":"CLEARED", "type":"UnifiedAp", "unifiedApInfo": { "instanceId": 0, "instanceVersion": 0, "apCertType": 1, "apGroupName":"xxx", "apMode": 2, "apStaticEnabled": 0, "bootVersion":"15.3.2.4", "capwapJoinTakenTime": 1500, "capwapUpTime": 52445240, "controllerIpAddress":"xxx", "controllerName":"wlc_5520", "contryCode":"US", "encryptionEnabled": false, "flexConnectMode": false, "iosVersion":"15.3(3)JBB6$", "linkLatencyEnabled": false, "poeStatus": 5, "portNumber": 8, "powerInjectorState": 1, "preStandardState": 0, "primaryMwar":"Cisco_10:2d:ae", "rogueDetectionEnabled": true, "sshEnabled": false, "statisticsTimer": 180, "telnetEnabled": false, "vlanEnabled": true, "vlanNativeId": 21, "WIPSEnabled": 0, "wlanVlanMappings": { "wlanVlanMapping": { "ssid":"xxx", "vlanId": 220, "wlanId": 1 } } }, "upTime": 2399985140 } } ] } } |
我需要创建一个新的JSON对象,上面只有一些信息,如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | "entity": [ { "@id": 12345, "name": asdf, "clienCount": 5, "clientCount_2_4GHz": 3, "clientCount_5GHz": 2 }, { "@id": 12345, "name": asdf, "clienCount": 5, "clientCount_2_4GHz": 3, "clientCount_5GHz": 2 } ] |
如果您查看,实体列表在原始数据的字典中。
我试图找到关于这个的信息,但是我只找到了如何删除,或者如何每次提取1个数据的信息。
我怎么能用Python做这个?
当做。
编辑:重复的帖子?如果是这样,你能链接到答案吗?因为我花了几个小时寻找答案?(不是为了袁基,谢谢你指出了这一独立的资源)
您的问题仅仅是python的组合,它从dict和filter dict列表中获取一个值列表,以便只包含某些键?
通过使用这两个问题的解决方案,您可以实现:
假设
1 2 | keys = ['@id', 'name', 'clientCount', 'clientCount_2_4GHz', 'clientCount_5GHz'] new_dict = {'entity': [{key: d['accessPointDetailsDTO'][key] for key in keys} for d in s['queryResponse']['entity']]} |
然后用
1 | your_json_object = json.dumps(new_dict) |
这很简单。
多恩