python - Neo4j Python bolt 驱动程序 : how to convert the result to Json?

标签 python flask neo4j type-conversion

我在 python 中使用 bolt 驱动程序 (1.0.1)。如何将结果转换为 Json,以便我可以通过 flask 应用程序将其返回? 那就是我需要将数据类型“neo4j.v1.types.Record”转换为“json”。

我试过了

from flask import Flask
from neo4j.v1 import GraphDatabase, basic_auth
import json

driver = GraphDatabase.driver("bolt://localhost:7687", auth=basic_auth("neo4j","neo4j"))
session = driver.session()
app = Flask(__name__)


@app.route('/hello/<prop>')
def hello_name(prop):
  result = session.run("MATCH ...") #this works perfectly fine and the data is captured in result
  session.close()
  for record in result:
    return json.loads(record)

这会引发错误:- TypeError:JSON 对象必须是 str,而不是“Record”

最佳答案

neo4j 驱动程序在生成器中给出结果。(记录类型)您不能执行 json.load,因为它不是 json 序列化字符串。

你可以做什么:

for record in result:
    print result[0]

查看结果的格式并取你喜欢的元素。

进一步了解如何解析 neo4j 记录: http://neo4j.com/docs/api/python-driver/current/session.html

关于python - Neo4j Python bolt 驱动程序 : how to convert the result to Json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43775604/

相关文章:

python - 按位 "&"ing 未指定数量的数据帧掩码?

python - 内部服务器错误 Flask

python - 使用 py2neo (Neo4j) 自动递增属性?

Neo4j Key-Value List 推荐实现

neo4j - 使用 LOAD CSV 加载数据时,如何仅当列具有值时才进行条件合并

python - 有没有办法在相邻元素唯一的条件下得到所有排列?

python - 表单未在 django admin 中保存电子邮件

python - *为什么* run_until_complete 不可重入。如何在没有线程的情况下逐步移植到异步?

python - 在 Tomcat 上使用 Jython 部署 Flask 应用程序的最佳方式是什么?

multithreading - Flask 与 PyQt5 - Flask 控制程序流程?