数据库是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 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,想要一个算法来转换它。