from flask import Flask, jsonify, render_template
from py2neo import Graph,authenticate
app = Flask(__name__)
authenticate("localhost:7474","neo4j", "neo4j")
graph = Graph("http://localhost:7474/db/data")
def buildNodes(nodeRecord):
print("............................")
data = {"id": str(nodeRecord.n._id), "label": next(iter(nodeRecord.n.labels))}
data.update(nodeRecord.n.properties)
print(data)
return {"data": data}
def buildEdges(relationRecord):
data = {"source": str(relationRecord.r.start_node._id),
"target": str(relationRecord.r.end_node._id),
"relationship": relationRecord.r.rel.type}
return {"data": data}
@app.route('/')
def index():
print("index")
return render_template('index.html')
@app.route('/graph')
def get_graph():
# print(graph.cypher.execute('MATCH (n) RETURN n').columns)
nodes = map(buildNodes, graph.cypher.execute('MATCH (n) RETURN n'))
print(nodes)
edges = map(buildEdges, graph.cypher.execute('MATCH()-[r]->() RETURN r'))
print(edges)
# json_2={"nodes":nodes,"edges":edges}
# return json.dumps(json_2, cls=UserEncoder)
elements = {"nodes": nodes, "edges": edges}
print(dict(elements))
return jsonify(elements)
return jsonify(elements)
if __name__ == '__main__':
app.run(debug = False)
當我使用Python連接圖形數據庫(Neo4j的),我有問題 'map object at 0x03D25C50' is not JSON serializable
,但map object at 0x03D25C50
是map()
方法的結果。我不知道如何解決這個問題。py2neo Neo4j的類型錯誤:<地圖在0x03D25C50對象>不是JSON序列
有沒有什麼明顯的東東在這裏錯了?
我不確定'map'對象是否實現了這個功能,但是許多對象實現了'__str__'方法,以便您可以獲取數據的字符串表示形式。有時它只是類名,但是例如'dict'對象會這樣做:>>> str({'b':2,'c':3}) 「{'c':3,'b ':2}'' –
非常感謝,我已經更改了代碼,但問題顯示如下,我期待着您的回答。萬分感謝! – kenneth