问题描述
我想漂亮地打印一个json文件,在其中我可以看到阵列ID。我正在运行带有运行Python(2.7.11)的NX-OS的Cisco Nexus交换机。看下面的代码:
cmd = 'show interface Eth1/1 counters'
out = json.loads(clid(cmd))
print (json.dumps(out,sort_keys=True,indent=4))
这给了我
{
"TABLE_rx_counters": {
"ROW_rx_counters": [
{
"eth_inbytes": "442370508663","eth_inucast": "76618907","interface_rx": "Ethernet1/1"
},{
"eth_inbcast": "4269","eth_inmcast": "49144","interface_rx": "Ethernet1/1"
}
]
},"TABLE_tx_counters": {
"ROW_tx_counters": [
{
"eth_outbytes": "217868085254","eth_outucast": "66635610","interface_tx": "Ethernet1/1"
},{
"eth_outbcast": "1137","eth_outmcast": "557815","interface_tx": "Ethernet1/1"
}
]
}
}
但是我需要通过以下方式访问该字段:
rxuc = int(out['TABLE_rx_counters']['ROW_rx_counters'][0]['eth_inucast'])
rxmc = int(out['TABLE_rx_counters']['ROW_rx_counters'][1]['eth_inmcast'])
rxbc = int(out['TABLE_rx_counters']['ROW_rx_counters'][1]['eth_inbcast'])
txuc = int(out['TABLE_tx_counters']['ROW_tx_counters'][0]['eth_outucast'])
txmc = int(out['TABLE_tx_counters']['ROW_tx_counters'][1]['eth_outmcast'])
txbc = int(out['TABLE_tx_counters']['ROW_tx_counters'][1]['eth_outbcast'])
因此,我需要知道阵列ID(在此示例中为零和一)以访问此接口的信息。似乎只有2个数组很容易,但想象有500个。现在,我总是将json代码复制到jsoneditoronline.org中,在那里我可以看到ID:
是否有一种简单的方法可以使ID在python本身中可见?
解决方法
您发布的是有效的JSON。
该图像来自一个工具,该工具从JSON获取数据并显示它。您可以按照任何想要的方式显示它,但是文件中的内容必须是有效的JSON。
如果以后不需要加载JSON,则可以随便使用它,但是json.dumps()
仅会提供JSON。