获得树形json串

public class TreeNode {

    private long nodeId;
    private String nodeName;
    private long fatherNodeId;

    public TreeNode() {
    }
    public TreeNode(long nodeId, String nodeName, long fatherNodeId) {
        this.nodeId = nodeId;
        this.nodeName = nodeName;
        this.fatherNodeId = fatherNodeId;
    }
    public long getNodeId() {
        return nodeId;
    }
    public void setNodeId(long nodeId) {
        this.nodeId = nodeId;
    }
    public String getNodeName() {
        return nodeName;
    }
    public void setNodeName(String nodeName) {
        this.nodeName = nodeName;
    }
    public long getFatherNodeId() {
        return fatherNodeId;
    }
    public void setFatherNodeId(long fatherNodeId) {
        this.fatherNodeId = fatherNodeId;
    }

}


public class ConstructorTree {
    StringBuffer json = new StringBuffer();
    /**
     * 
     * @Title: getJson
     * @Description: 调用接口
     * @author Administrator
     * @param list
     * @param node
     * @return
     */
    public String getJson(List<TreeNode> list, TreeNode node) {
        json = new StringBuffer();
        constructorJson(list, node);
        String jsonDate = json.toString();
        return ("[" + jsonDate + "]").replaceAll(",]", "]");


    }

    /**
     * 
     * @Title: constructorJson
     * @Description: 构建json树
     * @author Administrator
     * @param list
     * @param treeNode
     */
    @SuppressWarnings("unchecked")
    public void constructorJson(List<TreeNode> list, TreeNode treeNode) {
        if (hasChild(list, treeNode)) {
            json.append("{id:");
            json.append(treeNode.getNodeId());
            json.append(",text:");
            json.append(treeNode.getNodeName());
            json.append(",children:[");
            List<TreeNode> childList = getChildList(list, treeNode);
            Iterator iterator = childList.iterator();
            while (iterator.hasNext()) {
                TreeNode node = (TreeNode) iterator.next();
                constructorJson(list, node);
            }
            json.append("]},");
        } else {
            json.append("{id:");
            json.append(treeNode.getNodeId());
            json.append(",text:");
            json.append(treeNode.getNodeName());
            json.append("},");
        }
    }

    /**
     * 
     * @Title: getChildList
     * @Description: 获得子节点列表信息
     * @author Administrator
     * @param list
     * @param node
     * @return
     */
    public List<TreeNode> getChildList(List<TreeNode> list, TreeNode node) {
        List<TreeNode> li = new ArrayList<TreeNode>();
        Iterator it = list.iterator();
        while (it.hasNext()) {
            TreeNode n = (TreeNode) it.next();
            if (n.getFatherNodeId() == node.getNodeId()) {
                li.add(n);
            }
        }
        return li;
    }

    /**
     * 
     * @Title: hasChild
     * @Description: 判断是否有子节点
     * @author Administrator
     * @param list
     * @param node
     * @return
     */
    public boolean hasChild(List<TreeNode> list, TreeNode node) {
        return getChildList(list, node).size() > 0 ? true : false;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值