从neo4j rest API创建json的正确方法 [英] Correct way to create json from neo4j rest API

查看:109
本文介绍了从neo4j rest API创建json的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建以下json

I want to create the following json

"model":[
    {
        "_id": 123,
        "innerMode1": [
            {
                "_id": 233,
                "_type": "somtype2",
                "innerMode2": [
                    {
                        "_id": 24533,
                        "_type": "somtype3",
                        "innerMode2": [
                            { ....this goes on till depth of 6
                            }
                        ]
                    }
                ]
            }
        ], "_id": 45123,
        "innerMode2": [
            {
                "_id": 23433,
                "_type": "somtype2",
                "innerMode2": [
                    {
                        "_id": 241233,
                        "_type": "somtype3",
                        "innerMode2": [
                            {
                                ....this goes on till depth of 6
                            }
                        ]
                    }
                ]
            }
        ]
        .
        .
        .
        .
        .
        1000 records

密码类似:

MATCH (c:Capability{SectorID:{varSectorID}})-[r1:CapabilityAdopts_Capability]->(d)-[r2:Capability_Capability]->(e)-[r3:Capability_Capability]->(f)
OPTIONAL MATCH (f)<-[r3:Capability_Capability]-(g)
OPTIONAL MATCH (f)-[r4:KPI_Capability]-(h)
RETURN c,labels(c),r1,Type(r1),d,labels(d),r2,Type(r2),e,labels(e),r3,Type(r3),f,labels(f),r4,Type(r4),g,labels(g),r5,Type(r5),h,labels(h)

CapabilityAdopts_Capability = innerModel1,

CapabilityAdopts_Capability = innerModel1,

Capability_Capability = innerModel2(箭头的方向不同,因此数据也不同)

Capability_Capability = innerModel2 (direction of arrows are differnt hence data is different)

Capability_Capability = innerMode13(箭头的方向不同,因此数据也不同)

Capability_Capability = innerMode13 (direction of arrows are differnt hence data is different)

Capability_Capability = innerMode14(箭头的方向不同,因此数据也不同)

Capability_Capability = innerMode14 (direction of arrows are differnt hence data is different)


  var capObjectsContainer= {
          model: []
        };

var mainContainerL2 = jsonData.results[4].data; //this comes from neo after running the above cypher

for (i = 0; i < mainContainerL2.length; i++) {

    var isPartOfCapabilityContainer = {
        isPartOfCapability: []
    };

    var capIsMeasuredByKPIPatternContainer = {
        cpbltyIsMsrdByKpiPttrn: []
    };


    var consistsOfCapContainer = {
        consistsOfCapability: []
    };

    var isconsistsOfCap2Present = false;

    for (j = 0; j < capObjectsContainer.model.length; j++) {

        if (mainContainerL2[i].row[0]["ExternalID"] == capObjectsContainer.model[j]["_id"]) {

            for (k = 0; k < capObjectsContainer.model[j].adoptsCapability.length; k++) {

                if (mainContainerL2[i].row[4]["ExternalID"] == capObjectsContainer.model[j].adoptsCapability[k]["_id"]) {

                    for (l = 0; l < capObjectsContainer.model[j].adoptsCapability[k].consistsOfCapability.length; l++) {

                        if (mainContainerL2[i].row[8]["ExternalID"] == capObjectsContainer.model[j].adoptsCapability[k].consistsOfCapability[l]["_id"]) {

                            for (m = 0; m < capObjectsContainer.model[j].adoptsCapability[k].consistsOfCapability[l].consistsOfCapability.length; m++) {

                                if (mainContainerL2[i].row[12]["ExternalID"] == capObjectsContainer.model[j].adoptsCapability[k].consistsOfCapability[l].consistsOfCapability[m]["_id"]) {

                                    if (mainContainerL2[i].row[20] != null)
                                        AddcapIsMeasuredByKPIPattern(capObjectsContainer.model[j].adoptsCapability[k].consistsOfCapability[l].consistsOfCapability[m].cpbltyIsMsrdByKpiPttrn, mainContainerL2[i].row[20], mainContainerL2[i].row[21])

                                    if (mainContainerL2[i].row[16] != null && !IsExternalIdPresent(capObjectsContainer.model[j].adoptsCapability[k].consistsOfCapability[l].consistsOfCapability[m].isPartOfCapability, mainContainerL2[i].row[16]))
                                        AddIsPartOfCap(capObjectsContainer.model[j].adoptsCapability[k].consistsOfCapability[l].consistsOfCapability[m].isPartOfCapability, mainContainerL2[i].row[16], mainContainerL2[i].row[17])

                                    isconsistsOfCap2Present = true;
                                    break;
                                }
                            }
                            if (!isconsistsOfCap2Present) {
                                if (mainContainerL2[i].row[20] != null)
                                    AddcapIsMeasuredByKPIPattern(capIsMeasuredByKPIPatternContainer.cpbltyIsMsrdByKpiPttrn, mainContainerL2[i].row[20], mainContainerL2[i].row[21])

                                if (mainContainerL2[i].row[16] != null)
                                    AddIsPartOfCap(isPartOfCapabilityContainer.isPartOfCapability, mainContainerL2[i].row[16], mainContainerL2[i].row[17])

                                if (mainContainerL2[i].row[12] != null)
                                    AddConsistsOfCap(capObjectsContainer.model[j].adoptsCapability[k].consistsOfCapability[l].consistsOfCapability, mainContainerL2[i].row[12], mainContainerL2[i].row[13], isPartOfCapabilityContainer.isPartOfCapability, capIsMeasuredByKPIPatternContainer.cpbltyIsMsrdByKpiPttrn, consistsOfCapContainer.consistsOfCapability)

                            }
                            break;
                        }

                    }
                }
            }
        }
    }
}

要求不存在往返于Neo的往返行程.我只能打一次neo并获取数据.我不喜欢上面写的代码(尽管它完全按照我的意愿创建json).有没有更好的密码查询 我可以用来完成我想要的吗?我想摆脱所有循环. 在这里我可以使用的任何功能或属性可以为我提供帮助吗?

requirement is there cannot be a round trip to neo. I can hit neo only once and get data. I dont like the above code which i wrote (though it creates json exactly as I want). is there a better cypher query that I can use to accomplish what I want? I want to get rid of all the loops. Any functions that I can use or property that can help me here?

这是一个链接.

推荐答案

您可以使用collect + map和collection语法创建类似您在单个cypher语句中描述的内容,然后从最低级别开始,然后再进行操作沿着您的路径向上聚集.

You can use collect + map and collection syntax to create something like you describe in a single cypher statement, you'd start at the lowest level and then work your way upwards aggregating along your paths.

很遗憾,我无法听从您的解释.

Unfortunately I was not able to follow your explanation.

这里是指向文章的链接,对其进行了详细说明:

Here is a link to a article that explains it in detail:

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

这篇关于从neo4j rest API创建json的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆