Java 如何将树表转换为json树?
数据库是Oracle,该表有3列:id、parent_id、name,其中包含公司结构的数据Java 如何将树表转换为json树?,java,sql,json,algorithm,tree,Java,Sql,Json,Algorithm,Tree,数据库是Oracle,该表有3列:id、parent_id、name,其中包含公司结构的数据 id parent_id name 0 -1 CEO 1 0 CMO-marketing 2 0 COO - china 3 0 Mobile BU 4 0 COO - Intel 5 0
id parent_id name
0 -1 CEO
1 0 CMO-marketing
2 0 COO - china
3 0 Mobile BU
4 0 COO - Intel
5 0 WW - Sales
6 1 Research & Tech
7 1 Communications
8 1 Human Resources
9 2 PC BU
10 2 Sales
...
SQL查询:
select * from org connect by prior id = parent_id start with id = '0';
此SQL查询可以获取所有数据
我想将输出转换为json树
{"children":
[{
"id":0,
"leaf":false,
"text":"CEO",
"children":[
{
"text":"CMO-marketing",
"id":1,
"leaf":false,
"children":[
{
"text":"Research & Tech",
"id":6,
"leaf": true
},{
"text":"Communications",
"id":7,
"leaf": true
},{
"text":"Human Resources",
"id":8,
"leaf": true
}
]
},{
"text":"COO - china",
"id":2,
"leaf":false,
"children":[
{
"text":"PC BU",
"id":9,
"leaf": true
},{
"text":"Sales",
"id":10,
"leaf": true
}
]
},{
"text":"Mobile BU",
"id":3,
"leaf":false
},{
"text":"COO - Intel",
"id":4,
"leaf":false
},{
"text":"WW - Sales",
"id":5,
"leaf":false
}
]
}]
}
我使用java,并希望找到一种算法来转换它。我目前正试图解决多个连接表的类似问题。我没有为您提供Java解决方案,但是,似乎有许多非Java javascript解决方案可以在“单个”表的情况下工作。看看这些页面上的D3嵌套功能:@D.Woods您的工作非常出色,但我不知道如何为我塑造它。Javascript解决方案也适合我的项目,修改并不复杂。你的.csv数据不是一个树,不像我的表数据。你能说得更多吗?这些链接都不是我的工作。我还在处理这个问题。如果我能为我的多张桌子的情况想出一个像样的解决方案,我会和大家分享。