json树

本文介绍了一种将Java中的JSON数据转换为树形结构的方法,通过使用自定义的Tree类和BuildTree工具类实现。Tree类包含了节点的ID、状态、子节点等属性,而BuildTree工具类则负责构建树形结构,处理节点的父子关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JavaJson转Json树格式

1. 工具类

package com.tl.visual.util;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lc
 * @Date: 2018/11/21 15:51
 * json Tree工具类
 */
public class BuildTree {
    public static <T> Tree<T> build(List<Tree<T>> nodes) {

        if(nodes == null){
            return null;
        }
        List<Tree<T>> topNodes = new ArrayList<Tree<T>>();

        for (Tree<T> children : nodes) {

            String pid = children.getParentId();
            if (pid == null || "".equals(pid)) {
                topNodes.add(children);
                continue;
            }

            for (Tree<T> parent : nodes) {
                String id = parent.getId();
                if (id != null && id.equals(pid)) {
                    parent.getChildren().add(children);
                    continue;
                }
            }
        }
        Tree<T> root = new Tree<T>();
        if (topNodes.size() == 1) {
            root = topNodes.get(0);
        } else {
            root.setId("-1");
            root.setParentId("");
            root.setChildren(topNodes);
        }
        return root;
    }
}



package com.tl.visual.util;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lc
 * @Date: 2018/11/21 14:56
 */
public class Tree<T> {
    /**
     * 节点ID
     */
    private String id;
    /**
     * 显示节点文本
     */
    //private String text;
    /**
     * 节点状态
     */
    private String state;
    /**
     * 节点是否被选中 true false
     */
    //private boolean checked = false;
    /**
     * 节点属性
     */
    //private List<Map<String, Object>> attributes;
    /**
     * 节点的子节点
     */
    private List<Tree<T>> children = new ArrayList<Tree<T>>();

    /**
     * 父ID
     */
    private String parentId;
    /**
     * 分类名称
     */
    private String label;

    private String create_id;

    private String gmt_create;

    private String modified_id;

    private String gmt_modified;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public List<Tree<T>> getChildren() {
        return children;
    }

    public void setChildren(List<Tree<T>> children) {
        this.children = children;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getCreate_id() {
        return create_id;
    }

    public void setCreate_id(String create_id) {
        this.create_id = create_id;
    }

    public String getGmt_create() {
        return gmt_create;
    }

    public void setGmt_create(String gmt_create) {
        this.gmt_create = gmt_create;
    }

    public String getModified_id() {
        return modified_id;
    }

    public void setModified_id(String modified_id) {
        this.modified_id = modified_id;
    }

    public String getGmt_modified() {
        return gmt_modified;
    }

    public void setGmt_modified(String gmt_modified) {
        this.gmt_modified = gmt_modified;
    }



    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public Tree(String id, String state, List<Tree<T>> children, String parentId, String label, String create_id, String gmt_create, String modified_id, String gmt_modified) {
        this.id = id;
        this.state = state;
        this.children = children;
        this.parentId = parentId;
        this.label = label;
        this.create_id = create_id;
        this.gmt_create = gmt_create;
        this.modified_id = modified_id;
        this.gmt_modified = gmt_modified;
    }

    public Tree() {
        super();
    }

    @Override
    public String toString() {

        return JSON.toJSONString(this);
    }


 private JSONResponse getAll(){
        List<Tree<SystemRelate>> trees = new ArrayList<Tree<SystemRelate>>();
        List<SystemRelate> systemRelates = iSystemRelateService.queryAll();
        for(SystemRelate systemRelates1 : systemRelates){
            Tree<SystemRelate> tree = new Tree<SystemRelate>();
            tree.setId(systemRelates1.getClass_id());
            tree.setParentId(systemRelates1.getParent_id());
            tree.setState(systemRelates1.getState());
            tree.setLabel(systemRelates1.getClass_name());
            tree.setCreate_id(systemRelates1.getCreate_id());
            tree.setGmt_create(systemRelates1.getGmt_create().toString());
            if(systemRelates1.getGmt_modified()!=null){
                tree.setGmt_modified(systemRelates1.getGmt_modified().toString());
            }
            trees.add(tree);
        }
        Tree<SystemRelate> t = BuildTree.build(trees);
        return ResultUtil.success("",t);
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Insist_on_progress

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值