本方法源自:https://www.cnblogs.com/mzyn22/p/neo4j-query-data.html
但是在实际使用的过程中,发现有BUG,修改了一下,贴出来(代码中红色加粗的位置):
def query_graph(query): data = graph.run(query).data() node_ids = [] new_nodes = [] new_links = [] for a in data: for tk, tv in a.items(): nodes = tv.nodes relations = tv.relationships for n in nodes: if n.identity in node_ids: continue obj = {} obj["id"] = n.identity obj["label"] = [] if n.labels is not None: for la in n.labels: obj["label"].append(la) for k, v in n.items(): obj[k] = v node_ids.append(n.identity) new_nodes.append(obj) for r in relations: if r.identity in new_links: continue li = {} li["id"] = r.identity if r.types() is not None: li["label"] = [] for la in r.types(): li["label"].append(la) li["source"] = r.start_node.identity li["target"] = r.end_node.identity for k, v in r.items(): li[k] = vnode_ids.append(r.identity)# 此行去掉 new_links.append(li) result = {} result["nodes"] = new_nodes result["links"] = new_links return result