我有嵌套的数据,需要从中提取一些数据。由于嵌套数据的层次很深,我无法提取所需的所有数据。对于这个问题,我只关注如何获取对象节点所需的所有数据。
到目前为止,我仅提取键“ 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"
}
]
}
}
从这里我想拥有Node-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更新循环,但它仅返回最后的tpid值。根据源原始数据,openflow1和openflow2的tpid值具有3个值,而host:00:00:00:00:00:01和host:00:00:00:00:00:02的tpid值仅具有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
但它只是将每个节点添加1个tpid ... nodes_list1 = [[(dd ['id'],dd ['tpid'])for node_list中的dd]
谢谢
答案 0 :(得分:0)
尝试一下...并对其进行扩展
node_list = []
for nodes in restData["network-topology"]["topology"]:
for sub in nodes["node"]:
node = {}
node['id'] = sub['node-id']
for port in sub['termination-point']:
node['tpid'] = port['tp-id']
node_list.append(node)
它应该输出
[{'id': 'openflow:1', 'tpid': 'openflow:1:LOCAL'},
{'id': 'host:00:00:00:00:00:01', 'tpid': 'host:00:00:00:00:00:01'},
{'id': 'openflow:2', 'tpid': 'openflow:2:2'},
{'id': 'host:00:00:00:00:00:02', 'tpid': 'host:00:00:00:00:00:02'}]