1:如题
Java通过org.neo4j.driver调用Neo4j数据库时,返回结果是个StatementResult解析起来很费劲。
那么,如何将它转化为Json呢?
思路:
1. 转化为List :List list= session.run(cypherSql).list(Record::asMap);
2.用Gson转化为Json: JsonUtils.parses(GsonUtils.toJson(list));
代码块demo(注意,json也好,gson也罢,都是个人爱好,可以用相同类型的工具类代替)
/**
* 实现CypherSql
*
* @param cypherSql cypherSql语句
* @return
*/
public JsonObjects execute(String cypherSql) {
//获取会话
try (Session session = neo4jDriver.session()) {
//查询,并将结果转化为Jsons,返回
return JsonUtils.parses(GsonUtils.toJson(session.run(cypherSql).list(Record::asMap)));
} catch (Exception e) {
logger.error("实现cypherSql[{}]出现异常:", cypherSql, e);
return null;
}
}