private String createTreeJson(List<SchedJob> list) {
JSONArray rootArray = new JSONArray();
for (int i=0; i<list.size(); i++) {
SchedJob resource = list.get(i);
System.out.println(resource.getJobid()+"--------"+resource.getParentjobid());
//PARENT_JOB_ID=0 表示是根节点
if (resource.getParentjobid() == 0) {
JSONObject rootObj = createBranch(list, resource);
rootArray.add(rootObj);
}
}
return rootArray.toString();
}
/**
* 递归创建分支节点Json对象
* @param list 创建树的原始数据
* @param currentNode 当前节点
* @return 当前节点与该节点的子节点json对象
*/
private JSONObject createBranch(List<SchedJob> list, SchedJob currentNode) {
/*
* 将javabean对象解析成为JSON对象
*/
JSONObject currentObj = JSONObject.fromObject(currentNode);
JSONArray childArray = new JSONArray();
/*
* 循环遍历原始数据列表,判断列表中某对象的父id值是否等于当前节点的id值,
* 如果相等,进入递归创建新节点的子节点,直至无子节点时,返回节点,并将该
* 节点放入当前节点的子节点列表中
*/
for (int i=0; i<list.size(); i++) {
SchedJob newNode = list.get(i);
if (newNode.getParentjobid()!=null && newNode.getParentjobid().compareTo(currentNode.getJobid()) == 0) {
JSONObject childObj = createBranch(list, newNode);
childArray.add(childObj);
}
}
/*
* 判断当前子节点数组是否为空,不为空将子节点数组加入children字段中
*/
if (!childArray.isEmpty()) {
currentObj.put("children", childArray);
}
return currentObj;
}
MySQL树形查询结果封装成Json格式,返回给easyui treegrid