Neo4j Java bolt驱动程序:如何将结果转换为Json?

时间:2016-06-03 21:54:49

标签: java neo4j neo4j-bolt

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

我尝试以这种方式使用gson

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...

2 个答案:

答案 0 :(得分:5)

您展示的API不是Bolt-Driver,它是嵌入式Java-API。

在螺栓驱动程序中,你可以做到

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();

答案 1 :(得分:-1)

我正在使用flask开发一个应用程序,需要做同样的事情,然后将其放入响应中,但是使用Python。我使用jsonify而不是gson。有什么建议???代码在这里:

@concepts_api.route('/concepts', methods=['GET'])
  def get_concepts_of_conceptgroup():
  try: 
      _json = request.json
      _group_name = _json['group_name']
      if _group_name  and request.method == 'GET':
          rows = concepts_service.get_concepts_of_conceptgroup(_group_name)
          resp = jsonify(rows)
          resp.status_code = 200
          return resp
    
      return not_found() 
  except:
      message = {
      'status': 500,
      'message': 'Error: Imposible to get concepts of conceptgroup.',
      }
      resp = jsonify(message)
      resp.status_code = 500
      return resp
相关问题