简体   繁体   中英

Neo4j Java bolt driver: how to convert the result to Json?

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)?

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

but what I get is:

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

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

I am developing an app in flask and need to do the same and then get it into a response but in Python. Im using jsonify instead of gson. Any suggestions??? Code right here:

@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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM