Creating lists and dictionaries from JSON data in Python
本问题已经有最佳答案,请猛点这里访问。
我正在编写一个脚本,通过REST收集一些库存数据。然后我想过滤它来创建一个列表和两个字典,我可以在脚本的其他地方使用它们。
例如,从以下内容:
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 | {'version': '0.0' 'response': [{'chassisType': 'C800', 'family': 'C897VA-K9', 'hostname': 'chaney-xtr', 'imageName': 'c800-universalk9-mz.SPA.154-2.T.bin', 'interfaceCount': '10', 'lastUpdated': '2014-06-03 01:39:19.855491-07', 'lineCardId': 'e5bddd56-2194-4b83-8ae5-597893800051', 'macAddress': '88:5A:92:A4:E7:C8', 'managementIpAddress': '192.168.2.1', 'memorySize': '988236K/60339K', 'networkDeviceId': 'e15789bd-47df-4df9-809f-daf81d15ff2a', 'numUpdates': 1, 'platformId': 'C897VA-K9', 'portRange': 'ATM0, ATM0.1, BRI0, BRI0:1-2, Dialer1, Ethernet0, GigabitEthernet0-8, LISP0, Loopback0-1, NVI0, Virtual-Access1, Virtual-Template1, Vlan1', 'role': 'Unknown', 'roleSource': 'auto', 'serialNumber': 'FGL175124DX', 'softwareVersion': '15.4(2)T', 'type': 'UNKNOWN', 'upTime': '2 weeks, 3 days, 18 hours, 2 minutes', 'vendor': 'Cisco'}, {'chassisType': 'C800', 'family': 'C897VA-K9', 'hostname': 'chaney-xtr2', 'imageName': 'c800-universalk9-mz.SPA.154-2.T.bin', 'interfaceCount': '10', 'lastUpdated': '2014-06-03 01:39:19.855491-07', 'lineCardId': 'e5bddd56-2194-4b83-8ae5-597893800051', 'macAddress': '88:5A:92:A4:E7:C8', 'managementIpAddress': '192.168.2.2', 'memorySize': '988236K/60339K', 'networkDeviceId': 'e15789bd-47df-4df9-809f-daf81d15ff2a', 'numUpdates': 1, 'platformId': 'C897VA-K9', 'portRange': 'ATM0, ATM0.1, BRI0, BRI0:1-2, Dialer1, Ethernet0, GigabitEthernet0-8, LISP0, Loopback0-1, NVI0, Virtual-Access1, Virtual-Template1, Vlan1', 'role': 'Unknown', 'roleSource': 'auto', 'serialNumber': 'XGL175124D3', 'softwareVersion': '15.4(2)T', 'type': 'UNKNOWN', 'upTime': '2 weeks, 3 days, 18 hours, 2 minutes', 'vendor': 'Cisco'}], } |
在
以及两个使用IP地址作为密钥的字典
1 2 | dict1 = {"managementIpAddress":"hostname"} dict2 = {"managementIpAddress":"platformId"} |
你打算怎么做?
亲切的问候,
赖安
[cc lang="python"]from_this="version":"0.0","res响应":[;"macaaddress":"88:5a:92:92:A4:E7:C8","Network设备ID":"e5789bd-47df9-809f-daf81d5ff2a,",
from 123;"versi""""""""""""""",","versi"""""""""""""""""""""""""""""""""""""
您可以解析JSON数据,然后添加一些if和else案例来判断是否需要数据;然后使用eval语句生成dict