import java.util.ArrayList; import java.util.List; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class Test { public static void main(String[] args) { List<Sys_privilege> list = loadingList(); List<Sys_privilege> newList = refactorPriviList(list); JSONArray jsonArray = new JSONArray(); for (Sys_privilege sp : newList) { JSONObject jObject = new JSONObject(); jObject.put("id", sp.getId()); jObject.put("name", sp.getName()); jObject.put("subList", convertToJson(sp)); jsonArray.add(jObject); } System.out.println(jsonArray.toString()); } public static String convertToJson(Sys_privilege privi){ JSONArray jArray = new JSONArray(); for(Sys_privilege sp : privi.getSubPriviList()) { JSONObject jObject = new JSONObject(); jObject.put("id", sp.getId()); jObject.put("name", sp.getName()); jObject.put("subList", convertToJson(sp)); jArray.add(jObject); } return jArray.toString(); } public static List<Sys_privilege> refactorPriviList(List<Sys_privilege> list){ if(null == list || list.size() == 0) return null; Sys_privilege parent = new Sys_privilege(); parent.setParentId(0); List<Sys_privilege> subList = getPrivilegeByParentId(parent,list); for (Sys_privilege privilege : subList) { if(0==privilege.getParentId()){ getChildPrivilege(privilege,list); } } return subList; } /*** * 递归获取子权限 * @param privi * @param list */ public static void getChildPrivilege(Sys_privilege privi,List<Sys_privilege> list){ List<Sys_privilege> subList = getPrivilegeByParentId(privi,list); if(null != subList && subList.size() > 0){ for (Sys_privilege subPrivi : subList) { getChildPrivilege(subPrivi,list); } } privi.setSubPriviList(subList); } /*** * 从权限列表中获取指定parentId的权限 * @param priv * @param list * @return */ private static List<Sys_privilege> getPrivilegeByParentId(Sys_privilege priv,List<Sys_privilege> list){ if(null == list || list.size() == 0) return null; List<Sys_privilege> newList = new ArrayList<Sys_privilege>(); for (Sys_privilege sys_privilege : list) { if(sys_privilege.getParentId().equals(priv.getId())){ newList.add(sys_privilege); } } return newList; } public static List<Sys_privilege> loadingList(){ List<Sys_privilege> list = new ArrayList<Sys_privilege>(); Sys_privilege p = new Sys_privilege(); p.setId(1); p.setName("1"); p.setParentId(0); Sys_privilege p2 = new Sys_privilege(); p2.setId(2); p2.setName("2"); p2.setParentId(0); Sys_privilege p3 = new Sys_privilege(); p3.setId(3); p3.setName("3"); p3.setParentId(0); Sys_privilege p4 = new Sys_privilege(); p4.setId(4); p4.setName("1.1"); p4.setParentId(1); Sys_privilege p5 = new Sys_privilege(); p5.setId(5); p5.setName("1.2"); p5.setParentId(1); Sys_privilege p6 = new Sys_privilege(); p6.setId(6); p6.setName("1.1.1"); p6.setParentId(4); list.add(p); list.add(p2); list.add(p3); list.add(p4); list.add(p5); list.add(p6); return list; } }
import java.io.Serializable; import java.util.ArrayList; import java.util.List; import java.util.Set; public class Sys_privilege implements Serializable { private static final long serialVersionUID = 1L; /**权限状态:正常*/ public static final int PRIVILEGE_STATUS_NORMAL = 0; /**权限状态:停用*/ public static final int PRIVILEGE_STATUS_DISABLE = 1; /**权限类型:菜单类型*/ public static final String PRIVILEGE_TYPE_MENU = "0"; /**权限类型:按钮类型*/ public static final String PRIVILEGE_TYPE_BUTTON = "1"; private int id;// private String name;//权限名称 private String action;//对应action private Integer parentId;//父权限 private Integer status;//权限状态: 0.正常 1.停用 private String type;//权限类型: 0.菜单类型 1. 按钮类型 2... private List<Sys_privilege> subPriviList = new ArrayList<Sys_privilege>();//子权限 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAction() { return action; } public void setAction(String action) { this.action = action; } public Integer getParentId() { return parentId; } public void setParentId(Integer parentId) { this.parentId = parentId; } public Integer getStatus() { return status; } public void setStatus(Integer status) { this.status = status; } public Set<Sys_role> getRoles() { return roles; } public void setRoles(Set<Sys_role> roles) { this.roles = roles; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getQueryIds() { return queryIds; } public void setQueryIds(String queryIds) { this.queryIds = queryIds; } public List<Sys_privilege> getSubPriviList() { return subPriviList; } public void setSubPriviList(List<Sys_privilege> subPriviList) { this.subPriviList = subPriviList; } public String toString(){ return "[id=" + id + ",name=" + name + ",status=" + status + "type=" + type + ",parentId=" + parentId + ",action=" + action + "]"; } }
import java.util.ArrayList; import java.util.List; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class Test2 { public static void main(String[] args) { List<Sys_privilege> list = loadingList(); List<Sys_privilege> newList = refactorPriviList(list); System.out.println(newList.size()); } public static List<Sys_privilege> refactorPriviList(List<Sys_privilege> list){ if(null == list || list.size() == 0) return null; Sys_privilege parent = new Sys_privilege(); parent.setParentId(0); List<Sys_privilege> newList = new ArrayList<Sys_privilege>(); List<Sys_privilege> subList = getPrivilegeByParentId(parent,list); for (Sys_privilege privilege : subList) { if(0==privilege.getParentId()){ getChildPrivilege(privilege,list,newList); } } newList.addAll(subList); return newList; } /*** * 递归获取子权限 * @param privi * @param list */ public static void getChildPrivilege(Sys_privilege privi,List<Sys_privilege> list, List<Sys_privilege> newList){ List<Sys_privilege> subList = getPrivilegeByParentId(privi,list); if(null != subList && subList.size() > 0){ for (Sys_privilege subPrivi : subList) { getChildPrivilege(subPrivi,list,newList); } } newList.addAll(subList); } /*** * 从权限列表中获取指定parentId的权限 * @param priv * @param list * @return */ private static List<Sys_privilege> getPrivilegeByParentId(Sys_privilege priv,List<Sys_privilege> list){ if(null == list || list.size() == 0) return null; List<Sys_privilege> newList = new ArrayList<Sys_privilege>(); for (Sys_privilege sys_privilege : list) { if(sys_privilege.getParentId().equals(priv.getId())){ newList.add(sys_privilege); } } return newList; } public static List<Sys_privilege> loadingList(){ List<Sys_privilege> list = new ArrayList<Sys_privilege>(); Sys_privilege p = new Sys_privilege(); p.setId(1); p.setName("1"); p.setParentId(0); Sys_privilege p2 = new Sys_privilege(); p2.setId(2); p2.setName("2"); p2.setParentId(0); Sys_privilege p3 = new Sys_privilege(); p3.setId(3); p3.setName("3"); p3.setParentId(0); Sys_privilege p4 = new Sys_privilege(); p4.setId(4); p4.setName("1.1"); p4.setParentId(1); Sys_privilege p5 = new Sys_privilege(); p5.setId(5); p5.setName("1.2"); p5.setParentId(1); Sys_privilege p6 = new Sys_privilege(); p6.setId(6); p6.setName("1.1.1"); p6.setParentId(4); list.add(p); list.add(p2); list.add(p3); list.add(p4); list.add(p5); list.add(p6); return list; } }