简体   繁体   English

neo4j StatementResult到scala中的json

[英]neo4j StatementResult to json in scala

I'm new to scala. 我是斯卡拉的新手。 I'm building a REST-API to query several services, one of them Neo4j, using scala-2.11 I'm reading a neo4j output as StatementResult (org.neo4j.driver.v1 public interface StatementResult extends Iterator), this way: 我正在构建一个REST-API来查询几个服务,其中一个是Neo4j,使用scala-2.11我正在读取一个neo4j输出作为StatementResult(org.neo4j.driver.v1 public interface StatementResult extends Iterator),这样:

val recordsList = session.run(queryNeo4j)
println("recordsList:" +recordsList.list())

The println gives this output: println给出了这个输出:

recordsList:[Record<{n: node<6103>}>, Record<{n: node<6104>}>] recordsList:[Record <{n:node <6103>}>,Record <{n:node <6104>}>]

and trying to convert it into a json to send in the response, using play-json. 并尝试使用play-json将其转换为json以发送响应。 At first, I've tried this: 起初,我试过这个:

val recordJ = Json.parse(recordList)

but it's not working. 但它不起作用。 I've also tried this: 我也试过这个:

while (recordsList.hasNext() ) {
          val record = recordsList.next()
          println("record: " +record)
          val recordJ = Json.parse(record)
        }

But it's not working either.I get this error: 但它也没有工作。我得到这个错误:

error: overloaded method value parse with alternatives:
   [INFO]   (input: Array[Byte])play.api.libs.json.JsValue <and>
   [INFO]   (input: java.io.InputStream)play.api.libs.json.JsValue <and>
   [INFO]   (input: String)play.api.libs.json.JsValue
   [INFO]  cannot be applied to (org.neo4j.driver.v1.Record)
   [INFO]           val recordJ = Json.parse(record)
   [INFO]

Could someone please help me to get a json with the recordsList? 有人可以帮我一起使用recordsList获取json吗?

Thank you 谢谢

The easiest way would be to turn the statement result into a deep list of objects. 最简单的方法是将语句结果转换为深层对象列表。

val data = result.list(_.asMap())

And then format the result as JSON. 然后将结果格式化为JSON。

See the javadocs for each method. 请参阅每种方法的javadoc。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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