开发者

Flattening a JSON hierachy using Java

So I have a hierarchy of page objects and I am migrating from a Postgres System over to MongoDB, but I still need to support some legacy client systems which expect data in flat RDBMS style format.

If I store it in Mongo I have something kinda like this:

{
    "id": "1",
    "title": "Top Page",
    "children": 
    [
        {
            "id": "2",
            "title": "Page Two",
            "children": 
            [
                {
                    "id": "3",
                    "title": "Page Three",
                    "children": []
                }
            ]
        }
        {
            "id": "4",
            "title": "Page Four",
            "children": []
        }
    ]
}

But I need to reformat it, so the client apps can read it. And they expect it in a format like this:

[
{
    "id": "1",
    "title": "Top Page",
    "parentid": "top"
}
{
    "id": "2",
    "title": "Page Two",
    "parentid": "1"
}
{
    "id": "3",
    "title": "Page Three",
    "parentid": "2"
}
{
    "id": "4",
    "title": "Page Four开发者_运维知识库",
    "parentid": "1"
}
]

Is there an easy way to do this with Java or the MongoDB driver? or will I just have to iterate thru each of the objects and set the parentids manually? (some of these hierarchies are rather large).


There are 1000 ways to flatten a hierarchy, therefore I doubt there is an existing API to do that. Do it manually. It's a simple recursive method, after all.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜