集合数据转树结构
private JSONArray treeList(List<T> lists, String parentId) {
JSONArray childMenu = new JSONArray();
for (T model : lists) {
JSONObject jo = (JSONObject) JSONObject.toJSON(model);
if (parentId.equals(model.getParentId())) {
JSONArray node = treeList(lists, model.getId());
if (node!=null) {
jo.put("childNode", node);
}
childMenu.add(jo);
}
}
return childMenu;
}
public JSONArray treeMenuList(JSONArray menuList, int parentId) {
JSONArray childMenu = new JSONArray();
for (Object object : menuList) {
JSONObject jsonMenu = JSONObject.fromObject(object);
int menuId = jsonMenu.getInt("id");
int pid = jsonMenu.getInt("parentId");
if (parentId == pid) {
JSONArray c_node = treeMenuList(menuList, menuId);
jsonMenu.put("childNode", c_node);
childMenu.add(jsonMenu);
}
}
return childMenu;
}