java递归树状json

一、建立递归树的实体类

public class Tree implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 2944880335559089140L;

private String key;//节点key

private String title;//节点名称

private String parentKey;//节点父id

private String iconCls = "folder";//节点样式,默认即可

private Boolean isLeaf = true;//是否为叶子节点,true表示是叶子节点,false表示不是叶子节点

private Boolean expanded = true; //是否展开,默认true,展开

private List children;//孩子节点

/**
 * @取得id的值
 * @return the id
 */
public String getKey() {
    return key;
}

/**
 * @设置id的值
 * @param id the id to set
 */
public void setKey(String key) {
    this.key = key;
}

/**
 * 取得title的值
 *
 * @return  title值
 */

public String getTitle() {
    return title;
}

/**
 * 设定title的值
 *
 * @param  title 设定值
 */
public void setTitle(String title) {
    this.title = title;
}

/**
 * @取得parentId的值
 * @return the parentId
 */
public String getParentKey() {
    return parentKey;
}

/**
 * @设置parentId的值
 * @param parentId the parentId to set
 */
public void setParentKey(String parentKey) {
    this.parentKey = parentKey;
}

/**
 * @取得iconCls的值
 * @return the iconCls
 */
public String getIconCls() {
    return iconCls;
}

/**
 * @设置iconCls的值
 * @param iconCls the iconCls to set
 */
public void setIconCls(String iconCls) {
    this.iconCls = iconCls;
}

/**
 * @取得isLeaf的值
 * @return the isLeaf
 */
public Boolean getIsLeaf() {
    return isLeaf;
}

/**
 * @设置isLeaf的值
 * @param isLeaf the isLeaf to set
 */
public void setIsLeaf(Boolean isLeaf) {
    this.isLeaf = isLeaf;
}

/**
 * @取得expanded的值
 * @return the expanded
 */
public Boolean getExpanded() {
    return expanded;
}

/**
 * @设置expanded的值
 * @param expanded the expanded to set
 */
public void setExpanded(Boolean expanded) {
    this.expanded = expanded;
}

/**
 * @取得children的值
 * @return the children
 */
public List getChildren() {
    return children;
}

/**
 * @设置children的值
 * @param children the children to set
 */
public void setChildren(List children) {
    this.children = children;
}

}

二、递归的实现类

public class TreeUtil {

/**
 * 格式化list为树形list
 * @param list 需要格式化的list
 * @param flag true 表示全部展开,其他 表示不展开
 * @param  格式化的实体对象
 * @return 格式化之后的list
 */
public static  List formatTree(List list, Boolean flag) {

    List nodeList = new ArrayList();  
    for(T node1 : list){  
        boolean mark = false;  
        for(T node2 : list){  
            if(node1.getParentKey()!=null && node1.getParentKey().equals(node2.getKey())){ 
                node2.setIsLeaf(false);
                mark = true;  
                if(node2.getChildren() == null) {
                    node2.setChildren(new ArrayList());  
                }
                node2.getChildren().add(node1); 
                if (flag) {
                    //默认已经全部展开
                } else{
                    node2.setExpanded(false);
                }
                break;  
            }  
        }  
        if(!mark){  
            nodeList.add(node1);   
            if (flag) {
                //默认已经全部展开
            } else{
                node1.setExpanded(false);
            }
        }  
    }
    return nodeList;
}

}

注:需要递归的对应的实体信息,需继承Tree类

你可能感兴趣的:(java)