【发布时间】:2019-12-02 02:22:28
【问题描述】:
我有需要从中提取一些数据的嵌套数据。由于嵌套数据的深度级别..我无法提取所需的所有数据。对于这个问题,我只关注如何获取对象节点所需的所有数据。
到目前为止,我只提取了键“id”值...在嵌套数据中得到了一些其他键“tp-id”、“ip”和“mac”,我还需要获取这些值...但我仍然无法得到它....
这是原始数据(收集数据的源数据)
{
"network-topology": {
"topology": [
{
"node": [
{
"opendaylight-topology-inventory:inventory-node-ref": "/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:1']",
"node-id": "openflow:1",
"termination-point": [
{
"opendaylight-topology-inventory:inventory-node-connector-ref": "/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:1']/opendaylight-inventory:node-connector[opendaylight-inventory:id='openflow:1:2']",
"tp-id": "openflow:1:2"
},
{
"opendaylight-topology-inventory:inventory-node-connector-ref": "/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:1']/opendaylight-inventory:node-connector[opendaylight-inventory:id='openflow:1:1']",
"tp-id": "openflow:1:1"
},
{
"opendaylight-topology-inventory:inventory-node-connector-ref": "/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:1']/opendaylight-inventory:node-connector[opendaylight-inventory:id='openflow:1:LOCAL']",
"tp-id": "openflow:1:LOCAL"
}
]
},
{
"host-tracker-service:addresses": [
{
"first-seen": 1562655393902,
"ip": "10.0.0.1",
"mac": "00:00:00:00:00:01",
"id": 6,
"last-seen": 1562655393902
}
],
"host-tracker-service:id": "00:00:00:00:00:01",
"host-tracker-service:attachment-points": [
{
"active": true,
"corresponding-tp": "host:00:00:00:00:00:01",
"tp-id": "openflow:1:1"
}
],
"node-id": "host:00:00:00:00:00:01",
"termination-point": [
{
"tp-id": "host:00:00:00:00:00:01"
}
]
},
{
"opendaylight-topology-inventory:inventory-node-ref": "/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:2']",
"node-id": "openflow:2",
"termination-point": [
{
"opendaylight-topology-inventory:inventory-node-connector-ref": "/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:2']/opendaylight-inventory:node-connector[opendaylight-inventory:id='openflow:2:LOCAL']",
"tp-id": "openflow:2:LOCAL"
},
{
"opendaylight-topology-inventory:inventory-node-connector-ref": "/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:2']/opendaylight-inventory:node-connector[opendaylight-inventory:id='openflow:2:1']",
"tp-id": "openflow:2:1"
},
{
"opendaylight-topology-inventory:inventory-node-connector-ref": "/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:2']/opendaylight-inventory:node-connector[opendaylight-inventory:id='openflow:2:2']",
"tp-id": "openflow:2:2"
}
]
},
{
"host-tracker-service:addresses": [
{
"first-seen": 1562655393906,
"ip": "10.0.0.2",
"mac": "00:00:00:00:00:02",
"id": 7,
"last-seen": 1562655393906
}
],
"host-tracker-service:id": "00:00:00:00:00:02",
"host-tracker-service:attachment-points": [
{
"active": true,
"corresponding-tp": "host:00:00:00:00:00:02",
"tp-id": "openflow:2:1"
}
],
"node-id": "host:00:00:00:00:00:02",
"termination-point": [
{
"tp-id": "host:00:00:00:00:00:02"
}
]
}
],
"link": [
{
"link-id": "host:00:00:00:00:00:01/openflow:1:1",
"destination": {
"dest-node": "openflow:1",
"dest-tp": "openflow:1:1"
},
"source": {
"source-tp": "host:00:00:00:00:00:01",
"source-node": "host:00:00:00:00:00:01"
}
},
{
"link-id": "openflow:2:1/host:00:00:00:00:00:02",
"destination": {
"dest-node": "host:00:00:00:00:00:02",
"dest-tp": "host:00:00:00:00:00:02"
},
"source": {
"source-tp": "openflow:2:1",
"source-node": "openflow:2"
}
},
{
"link-id": "openflow:1:2",
"destination": {
"dest-node": "openflow:2",
"dest-tp": "openflow:2:2"
},
"source": {
"source-tp": "openflow:1:2",
"source-node": "openflow:1"
}
},
{
"link-id": "openflow:2:2",
"destination": {
"dest-node": "openflow:1",
"dest-tp": "openflow:1:2"
},
"source": {
"source-tp": "openflow:2:2",
"source-node": "openflow:2"
}
},
{
"link-id": "openflow:1:1/host:00:00:00:00:00:01",
"destination": {
"dest-node": "host:00:00:00:00:00:01",
"dest-tp": "host:00:00:00:00:00:01"
},
"source": {
"source-tp": "openflow:1:1",
"source-node": "openflow:1"
}
},
{
"link-id": "host:00:00:00:00:00:02/openflow:2:1",
"destination": {
"dest-node": "openflow:2",
"dest-tp": "openflow:2:1"
},
"source": {
"source-tp": "host:00:00:00:00:00:02",
"source-node": "host:00:00:00:00:00:02"
}
}
],
"topology-id": "flow:1"
}
]
}
}
从这里我想要节点 - id、tpid、ip 和 mac... 到目前为止只有 id。
请在下面找到我用来获取 id 值的代码。我尝试使用 for 循环获取 tpid,但我也卡住了....
#Extract nodes info
node_list = []
for nodes in restData["network-topology"]["topology"][0]["node"]:
node = {}
node['id'] = nodes['node-id']
#for port in nodes['termination-point']:
#node['tpid'] = port['tp-id']
node_list.append(node)
nodes_list1 = [dd['id'] for dd in node_list]
print nodes_list1
#Add nodes_list1 info onto networkx
graph = nx.Graph()
graph.add_nodes_from(nodes_list1)
#Write onto json file
d = json_graph.node_link_data(graph)
with open('mytopo.json', 'w') as fc:
json.dump(d, fc, indent=4)
这是 mytopo.json 的输出
{
"directed": false,
"graph": {},
"nodes": [
{
"id": "host:00:00:00:00:00:02"
},
{
"id": "openflow:1"
},
{
"id": "openflow:2"
},
{
"id": "host:00:00:00:00:00:01"
}
],
"links": [],
"multigraph": false
}
我希望在 json 文件中包含 tpid、ip 和 mac...我希望 mytopo.json 如下所示
{
"directed": false,
"graph": {
},
"nodes": [
{
"id": "host:00:00:00:00:00:01",
"tpid": "host:00:00:00:00:00:01",
"ip": "10.0.0.1",
"mac": "00:00:00:00:00:01"
},
{
"id": "openflow:1",
"tpid": [
"openflow:1:2",
"openflow:1:1",
"openflow:1:LOCAL"
]
},
{
"id": "openflow:2",
"tpid": [
"openflow:2:LOCAL",
"openflow:2:1",
"openflow:2:2"
]
},
{
"id": "host:00:00:00:00:00:02",
"tpid": "host:00:00:00:00:00:02",
"ip": "10.0.0.2",
"mac": "00:00:00:00:00:02"
}
],
"links": [
],
"multigraph": false
}
感谢您的专家为我提供进一步的帮助和指导。谢谢。
我根据输入 @Tim Seed 更新 for 循环,但它只返回最后一个 tpid 值。根据源原始数据,openflow1 和 openflow2 的 tpid 值有 3 个值,而 host:00:00:00:00:00:01 和 host:00:00:00:00:00:02 只有 1 个 tpid 值...所以它应该返回如下所示的内容
nodes": [
{
"id": "host:00:00:00:00:00:01",
"tpid": "host:00:00:00:00:00:01",
"ip": "10.0.0.1",
"mac": "00:00:00:00:00:01"
},
{
"id": "openflow:1",
"tpid": [
"openflow:1:2",
"openflow:1:1",
"openflow:1:LOCAL"
]
},
{
"id": "openflow:2",
"tpid": [
"openflow:2:LOCAL",
"openflow:2:1",
"openflow:2:2"
]
},
{
"id": "host:00:00:00:00:00:02",
"tpid": "host:00:00:00:00:00:02",
"ip": "10.0.0.2",
"mac": "00:00:00:00:00:02"
}
],
当我打印 node['tpid']... 它会显示所有的 tpid 如下
openflow:1:2
openflow:1:1
openflow:1:LOCAL
host:00:00:00:00:00:01
openflow:2:LOCAL
openflow:2:1
openflow:2:2
host:00:00:00:00:00:02
但它只在 node_list 上每个添加 1 个 tpid... nodes_list1 = [(dd['id'], dd['tpid']) for dd in node_list]
谢谢
【问题讨论】:
标签: python json list dictionary nested