Python:具有数组ID的漂亮打印json文件

问题描述

我想漂亮地打印一个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:

enter image description here

是否有一种简单的方法可以使ID在python本身中可见?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)