本文目录一览:
- 1、java 递归数据库生成 树形结构问题
- 2、java实现树形结构,可以把String[] a = {"1", "1-3-4", "1-2", "1-2"}这个数组,转变成如图的树json么?
- 3、如何用Java拼接JSON方式遍历整个树形节点
- 4、javaweb里面树形结构(tree)
java 递归数据库生成 树形结构问题
1、准备表结构及对应的表数据
a、表结构:
create table TB_TREE
(
CID NUMBER not null,
CNAME VARCHAR2(50),
PID NUMBER //父节点
)
b、表数据:
insert into tb_tree (CID, CNAME, PID) values (1, '中国', 0);
insert into tb_tree (CID, CNAME, PID) values (2, '北京市', 1);
insert into tb_tree (CID, CNAME, PID) values (3, '广东省', 1);
insert into tb_tree (CID, CNAME, PID) values (4, '上海市', 1);
insert into tb_tree (CID, CNAME, PID) values (5, '广州市', 3);
insert into tb_tree (CID, CNAME, PID) values (6, '深圳市', 3);
insert into tb_tree (CID, CNAME, PID) values (7, '海珠区', 5);
insert into tb_tree (CID, CNAME, PID) values (8, '天河区', 5);
insert into tb_tree (CID, CNAME, PID) values (9, '福田区', 6);
insert into tb_tree (CID, CNAME, PID) values (10, '南山区', 6);
insert into tb_tree (CID, CNAME, PID) values (11, '密云县', 2);
insert into tb_tree (CID, CNAME, PID) values (12, '浦东', 4);
2、TreeNode对象,对应tb_tree
public class TreeNode implements Serializable {
private Integer cid;
private String cname;
private Integer pid;
private List nodes = new ArrayList();
public TreeNode() {
}
//getter、setter省略
}
3、测试数据
public class TreeNodeTest {
@Test
public void loadTree() throws Exception{
System.out.println(JsonUtils.javaToJson(recursiveTree(1)));
}
/**
* 递归算法解析成树形结构
*
* @param cid
* @return
* @author jiqinlin
*/
public TreeNode recursiveTree(int cid) {
//根据cid获取节点对象(SELECT * FROM tb_tree t WHERE t.cid=?)
TreeNode node = personService.getreeNode(cid);
//查询cid下的所有子节点(SELECT * FROM tb_tree t WHERE t.pid=?)
List childTreeNodes = personService.queryTreeNode(cid);
//遍历子节点
for(TreeNode child : childTreeNodes){
TreeNode n = recursiveTree(child.getCid()); //递归
node.getNodes().add(n);
}
return node;
}
}
输出的json格式如下:
{
"cid": 1,
"nodes": [
{
"cid": 2,
"nodes": [
{
"cid": 11,
"nodes": [
],
"cname": "密云县",
"pid": 2
}
],
"cname": "北京市",
"pid": 1
},
{
"cid": 3,
"nodes": [
{
"cid": 5,
"nodes": [
{
"cid": 7,
"nodes": [
],
"cname": "海珠区",
"pid": 5
},
{
"cid": 8,
"nodes": [
],
"cname": "天河区",
"pid": 5
}
],
"cname": "广州市",
"pid": 3
},
{
"cid": 6,
"nodes": [
{
"cid": 9,
"nodes": [
],
"cname": "福田区",
"pid": 6
},
{
"cid": 10,
"nodes": [
],
"cname": "南山区",
"pid": 6
}
],
"cname": "深圳市",
"pid": 3
}
],
"cname": "广东省",
"pid": 1
},
{
"cid": 4,
"nodes": [
{
"cid": 12,
"nodes": [
],
"cname": "浦东",
"pid": 4
}
],
"cname": "上海市",
"pid": 1
}
],
"cname": "中国",
"pid": 0
}
java实现树形结构,可以把String[] a = {"1", "1-3-4", "1-2", "1-2"}这个数组,转变成如图的树json么?
//先定义一个类,方便转换
public class Menu {
private Long id;
private List child = new ArrayList();
public Menu(Long id) {
this.id = id;
}
}
public class String2JsonTest {
public static void main(String[] args) {
String[] a = {"1", "1-3-2", "1-2", "1-2"};
List list2 = new ArrayList();
for (int i = 0; i a.length; i++) {
String[] b = a[i].split("-");
List tempList = list2;
Menu tempMenu = null;
for (int j = 0; j b.length; j++) {
tempMenu = new Menu(Long.valueOf(b[j]));
Menu exist = isExist(tempMenu,tempList);
if(exist==null){
tempList.add(tempMenu);
tempList = tempMenu.getChild();
}else{
tempList = exist.getChild();
}
}
}
String jsonArray2 = JSONArray.toJSONString(list2);
System.out.println(jsonArray2);
}
private static Menu isExist(Menu menu,ListMenu list){
for (Menu obj : list) {
if(menu.getId().equals(obj.getId())){
return obj;
}
}
return null;
}
}
如何用Java拼接JSON方式遍历整个树形节点
//是类似这种吗
//控制层使用JSONArray jsonObject=JSONArray.fromObject();转换
MapString,Object map = new HashMapString, Object();
map.put("id","1");
map.put("text","实验外国语学校");
ListMapString,Object fatherList=new ArrayListMapString,Object();
ListMapString,Object list=new ArrayListMapString,Object();
for(Bean bean:list){
if("1".equals(list.getParent_level())){
MapString,Object map2=new HashMapString, Object();
map2.put("id",list.getId());
map2.put("text",list.getName());
list.add(map2);
}
}
map.put("children",list);
javaweb里面树形结构(tree)
这个是java中的forEach循环,和
for(int i =0 ;i 10 ;i++){...}
还是有点区别的。有问题可以继续 问。