根据pid和id获取树形json

这篇博客介绍了如何在Java中根据pid和id构建树形JSON数据。首先,提到了项目的maven依赖,接着展示了部门实体类的代码示例,并说明由于功能简单,整个实现仅涉及一个类。最终,博主展示了一个以JSON格式输出的部门列表。

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

maven依赖

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.qianyz</groupId>
  <artifactId>SimpleDemo</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>SimpleDemo</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.24</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>com.googlecode.json-simple</groupId>
      <artifactId>json-simple</artifactId>
      <version>1.1.1</version>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>8</source>
          <target>8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

拿部门举例,实体类代码如下

package com.qianyz.convertListToTree;

import lombok.Builder;
import lombok.Data;

import java.time.LocalDateTime;

@Data
@Builder
public class Department {
    Integer id;
    Integer pid;
    String name;
    String departmentCode;
    LocalDateTime createTime;
}

功能简单剩下就一个类了

package com.qianyz.convertListToTree;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        List<Department> list = init();
        Map<Integer,List<Department>> map = new HashMap<>();
        for(Department department : list){
            List<Department> tempList = map.get(department.getPid());
            if(tempList==null){
                tempList = new ArrayList<>();
            }
            tempList.add(department);
            map.put(department.getPid(),tempList);
        }
        List l = getReturn(-1,map);
        JSONObject json = new JSONObject();
        System.out.println(l);
        System.out.println(JSONArray.toJSONString(l));
    }

    public static List<Map<String,Object>> getReturn(Integer pid, Map<Integer,List<Department>> map){
        List<Map<String,Object>> returnList = new ArrayList<>();
        List<Department> list = map.get(pid);
        for(Department department : list){
            Map<String,Object> hashMap = new HashMap<>();
            hashMap.put("id",department.getId());
            hashMap.put("pid",department.getPid());
            hashMap.put("name",department.getName());
            hashMap.put("departmentCode",department.getDepartmentCode());
            hashMap.put("createTime",department.getCreateTime().toString());
            if(map.containsKey(department.getId())){
                hashMap.put("children",getReturn(department.getId(),map));
            }
            returnList.add(hashMap);
        }
        return returnList;
    }

    public static List<Department> init() {
        List<Department> list = new ArrayList<>();
        list.add(Department.builder().id(1).pid(-1).name("A公司").departmentCode("1000").createTime(LocalDateTime.now()).build());
        list.add(Department.builder().id(2).pid(1).name("部门一").departmentCode("1100").createTime(LocalDateTime.now()).build());
        list.add(Department.builder().id(3).pid(1).name("部门二").departmentCode("1200").createTime(LocalDateTime.now()).build());
        list.add(Department.builder().id(4).pid(1).name("部门三").departmentCode("1300").createTime(LocalDateTime.now()).build());
        list.add(Department.builder().id(5).pid(2).name("小组1").departmentCode("1110").createTime(LocalDateTime.now()).build());
        list.add(Department.builder().id(6).pid(2).name("小组2").departmentCode("1120").createTime(LocalDateTime.now()).build());
        list.add(Department.builder().id(7).pid(2).name("小组3").departmentCode("1130").createTime(LocalDateTime.now()).build());
        list.add(Department.builder().id(8).pid(2).name("小组4").departmentCode("1140").createTime(LocalDateTime.now()).build());
        list.add(Department.builder().id(9).pid(3).name("小队1").departmentCode("1210").createTime(LocalDateTime.now()).build());
        list.add(Department.builder().id(10).pid(3).name("小队2").departmentCode("1220").createTime(LocalDateTime.now()).build());
        list.add(Department.builder().id(11).pid(3).name("小队3").departmentCode("1230").createTime(LocalDateTime.now()).build());
        list.add(Department.builder().id(12).pid(3).name("小队4").departmentCode("1240").createTime(LocalDateTime.now()).build());
        list.add(Department.builder().id(13).pid(4).name("Team1").departmentCode("1310").createTime(LocalDateTime.now()).build());
        list.add(Department.builder().id(14).pid(4).name("Team2").departmentCode("1320").createTime(LocalDateTime.now()).build());
        list.add(Department.builder().id(15).pid(4).name("Team3").departmentCode("1330").createTime(LocalDateTime.now()).build());
        list.add(Department.builder().id(16).pid(4).name("Team4").departmentCode("1340").createTime(LocalDateTime.now()).build());
        return list;
    }

}

最后输出了JSON格式的列表,暂时就先这样

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值