我有嵌套数据,需要从中提取一些数据。由于嵌套数据的深度级别.我无法提取所有需要的数据。对于这个问题,我只关注如何获取对象节点所需的所有数据。
到目前为止,我已经提取了键'id‘值only...in嵌套的数据得到了一些'tp-id','ip’和'mac‘的其他键,我需要得到的值also...but,我仍然无法得到它.
这是原始数据(收集数据的源数据)。
{
"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值的代码。我试着得到tpid,例如使用for循环,但我也坚持住了.
#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
}我希望有tpid、ip和mac在json文件中.我希望有如下所示的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种子更新for循环,但它只返回最后一个tpid值。根据源原始数据,openflow1和openflow2的tpid值有3个值,而主机:00:00:00:00:00:01和主机:00:00:00:00:00:02只有一个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"
}
],当我打印节点‘’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上每个添加了一个tpid .nodes_list1 =[( dd‘in’,dd‘’tpid‘)用于node_list中的dd]
谢谢
发布于 2019-07-24 02:23:13
试试这个..。并对其进行扩展
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'}]https://stackoverflow.com/questions/57174302
复制相似问题