使用Java展平JSON层次结构
因此,我有一个页面对象的层次结构,我正在从Postgres系统迁移到MongoDB,但我仍然需要支持一些传统的客户端系统,这些系统希望数据采用平面RDBMS样式的格式 如果我将其存储在Mongo中,我会有类似的东西:使用Java展平JSON层次结构,java,json,mongodb,Java,Json,Mongodb,因此,我有一个页面对象的层次结构,我正在从Postgres系统迁移到MongoDB,但我仍然需要支持一些传统的客户端系统,这些系统希望数据采用平面RDBMS样式的格式 如果我将其存储在Mongo中,我会有类似的东西: { "id": "1", "title": "Top Page", "children": [ { "id": "2", "title": "Page Two",
{
"id": "1",
"title": "Top Page",
"children":
[
{
"id": "2",
"title": "Page Two",
"children":
[
{
"id": "3",
"title": "Page Three",
"children": []
}
]
}
{
"id": "4",
"title": "Page Four",
"children": []
}
]
}
[
{
"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"
}
]
但我需要重新格式化它,以便客户端应用程序可以读取它。他们希望它的格式如下:
{
"id": "1",
"title": "Top Page",
"children":
[
{
"id": "2",
"title": "Page Two",
"children":
[
{
"id": "3",
"title": "Page Three",
"children": []
}
]
}
{
"id": "4",
"title": "Page Four",
"children": []
}
]
}
[
{
"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"
}
]
使用Java或MongoDB驱动程序有没有一种简单的方法可以做到这一点?或者我只需要迭代每个对象并手动设置parentId?(其中一些层次结构相当大)。有1000种方法可以展平层次结构,因此我怀疑现有的API是否可以做到这一点。手动操作。毕竟,这是一种简单的递归方法