獲得樹形json串

來源:互聯網
上載者:User

標籤: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;
    }
}

獲得樹形json串

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.