Neo4j Java bolt 驱动程序:如何将结果转换为 Json? [英] Neo4j Java bolt driver: how to convert the result to Json?

查看:162
本文介绍了Neo4j Java bolt 驱动程序:如何将结果转换为 Json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Java Bolt 驱动程序 (1.0.1),我想知道有没有办法将结果转换为 Json(可能与 REST api 中的相同)?

I am using the Java Bolt driver (1.0.1) and I am wondering there is a way to convert the result to Json (possibly the same as in the REST api)?

我尝试以这种方式使用 gson:

I tried to use gson in this way:

Result r = null;
try ( Transaction tx = graphDb.beginTx() )
{
    r = graphDb.execute("MATCH...");
    tx.success();
} catch {...}

new Gson().toJson(result);

但我得到的是:

java.lang.StackOverflowError
    at com.google.gson.internal.$Gson$Types.canonicalize($Gson$Types.java:98)
    at com.google.gson.reflect.TypeToken.<init>(TypeToken.java:72)
    etc...

推荐答案

您展示的 API 不是 Bolt-Driver,而是嵌入式 Java-API.

The API you show is not the Bolt-Driver, it's the embedded Java-API.

在螺栓驱动器中你可以做

In the bolt-driver you can do

Driver driver = GraphDatabase.driver( "bolt://localhost", AuthTokens.basic( "neo4j", "neo4j" ) );
Session session = driver.session();

StatementResult result = session.run( "MATCH (a:Person) WHERE a.name = 'Arthur' RETURN a.name AS name, a.title AS title" );

while ( result.hasNext() ) {
    Record record = result.next();
    gson.toJson(record.asMap());
}
session.close();
driver.close();

这篇关于Neo4j Java bolt 驱动程序:如何将结果转换为 Json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆