Python:带有数组 ID 的漂亮打印 json 文件
Python: Pretty print json file with array ID's
我想打印一个 json 文件,我可以在其中看到数组 ID。我正在使用 NX-OS 运行 Python (2.7.11) 的 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。
我想打印一个 json 文件,我可以在其中看到数组 ID。我正在使用 NX-OS 运行 Python (2.7.11) 的 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。