parsing树结果/得到儿童

我目前正在开发一个新的应用程序,我需要pipe理位置(大洲>国家>地区>城市)。

我试图(没有成功)使用密码从neo4j直接获取树:

MATCH p = (r:Location)-[:CONTAINS*]->() WHERE r.category='continent' RETURN p AS path 

现在,我想parsing这些数据,所以我可以这样做:

 MATCH (r:Location)-[:CONTAINS]->(r2) WHERE r.category='continent' OPTIONAL MATCH (r2)-[:CONTAINS]->(r3) OPTIONAL MATCH (r3)-[:CONTAINS]->(r4) OPTIONAL MATCH (r4)-[:CONTAINS]->(r5) OPTIONAL MATCH (r6)-[:CONTAINS]->(r6) RETURN r.name, r.category, r2.name, r2.category, r3.name, r3.category, r4.name,r4.category, r5.name, r5.category, r6.name, r6.category 

但这不是我等待的回应,我不喜欢这个解决scheme,因为最多有6个关系。 如果我这样做,我冒险破坏树。

 [ { 'r.name': 'Europe','r.category': 'continent','r2.name': 'Germany','r2.category': 'country','r3.name': null,'r3.category': null,'r4.name': null,'r4.category': null,'r5.name': null,'r5.category': null,'r6.name': null,'r6.category': null }, { 'r.name': 'Europe','r.category': 'continent','r2.name': 'France','r2.category': 'country','r3.name': 'Ile de France','r3.category': 'region','r4.name': 'Paris','r4.category': 'city','r5.name': null,'r5.category': null,'r6.name': null,'r6.category': null }, { 'r.name': 'Europe','r.category': 'continent','r2.name': 'France','r2.category': 'country','r3.name': 'Ile de France','r3.category': 'region','r4.name': 'Versailles','r4.category': 'city','r5.name': null,'r5.category': null,'r6.name': null,'r6.category': null }, { 'r.name': 'Europe','r.category': 'continent','r2.name': 'France','r2.category': 'country','r3.name': 'Ile de France','r3.category': 'region','r4.name': 'Montreuil','r4.category': 'city','r5.name': null,'r5.category': null,'r6.name': null,'r6.category': null }, { 'r.name': 'Europe','r.category': 'continent','r2.name': 'Belgium','r2.category': 'country','r3.name': null,'r3.category': null,'r4.name': null,'r4.category': null,'r5.name': null,'r5.category': null,'r6.name': null,'r6.category': null } ] 

有没有一种方法来parsing结果/改变我的查询得到一个JSON格式的树与'儿童'像这样:

 [{"name":"Africa"},{"name":"Europe","childrens":[{"name":"France"},{"name":"Germany"},{"name":"Belgium","childrens":[{"name":"Brussels Capitale","childrens":[{"name":"Brussels Capitale"}]}]}]}] 

谢谢你的帮助 !

弗雷德。

是的,你可以使用非常好的谷歌GSON JSON库来创build你想要的JSON。 其中一些库非常善于使用vanilla数据结构,并将它们转换为JSON对象。

所以当你在java中执行一个密码查询的时候,你从ExecutionEngine对象中得到的结果就是一个ExecutionResult对象 – 它与Maptypes兼容。 从本质上讲,如果您使用Cypher查询来返回正确的数据,则可以将执行结果直接inputGSON。 如何使用GSON将复杂的Java对象转换为JSON的例子在这里 。

另一种方法是使用Neo4J REST API通过POST操作将Cypher查询发送到服务器,然后尝试按摩发回给您的JSON。 我不认为它会把你想要的东西发给你,但你可以非常接近,然后从那里使用这种格式。

使用Neo4j 2.0,您还可以在Cypher中使用文字地图和集合,因此您可以根据自己的喜好构build自己的JSON文档,以此GraphGist为例。

http://gist.neo4j.org/?9269173

好的…我find了解决办法:)

 MATCH (l:Location) WHERE l.category='continent' OPTIONAL MATCH (l)-[:CONTAINS]->(l2:Location) OPTIONAL MATCH (l2)-[:CONTAINS]->(l3:Location) OPTIONAL MATCH (l3)-[:CONTAINS]->(l4:Location) OPTIONAL MATCH (l4)-[:CONTAINS]->(l5:Location) WITH l AS l, l2 AS l2, l3 AS l3, l4 AS l4, COLLECT( DISTINCT { name: l5.name }) AS subl4 WITH l AS l, l2 AS l2, l3 AS l3, subl4, COLLECT( DISTINCT { name: l4.name, children: subl4 }) AS subl3 WITH l AS l, l2 AS l2, subl3 AS subl3, COLLECT( DISTINCT { name: l3.name, children: subl3 }) AS subl2 WITH l AS l, COLLECT( DISTINCT { name: l2.name, children: subl2 }) AS subl RETURN COLLECT( DISTINCT { name: l.name, children: subl })