首页 > 其他分享 >类中转对应json结构

类中转对应json结构

时间:2023-03-27 23:23:10浏览次数:43  
标签:value String 中转 private ApiModelProperty json 科室 Department 对应

一、实体类

List对应JSONArray[],对象对应JSONObject{}

@Data
@ApiModel(description = "Department")
@Document("Department")
public class Department extends BaseMongoEntity {
	
	private static final long serialVersionUID = 1L;

	@ApiModelProperty(value = "医院编号")
	@Indexed //普通索引
	private String hoscode;

	@ApiModelProperty(value = "科室编号")
	@Indexed(unique = true) //唯一索引
	private String depcode;

	@ApiModelProperty(value = "科室名称")
	private String depname;

	@ApiModelProperty(value = "科室描述")
	private String intro;

	@ApiModelProperty(value = "大科室编号")
	private String bigcode;

	@ApiModelProperty(value = "大科室名称")
	private String bigname;

}

@Data
@ApiModel(description = "Department")
public class DepartmentVo {

	@ApiModelProperty(value = "科室编号")
	private String depcode;

	@ApiModelProperty(value = "科室名称")
	private String depname;

	@ApiModelProperty(value = "下级节点")
	private List<DepartmentVo> children;

}

二、方法

public List<DepartmentVo> findDeptTree(String hoscode) {
        //创建list集合,用于最终数据封装
        List<DepartmentVo> result = new ArrayList<>();

        //根据医院编号,查询医院所有科室信息
        Department departmentQuery = new Department();
        departmentQuery.setHoscode(hoscode);
        Example example = Example.of(departmentQuery);
        //所有科室列表 departmentList
        List<Department> departmentList = departmentRepository.findAll(example);

        //根据大科室编号  bigcode 分组,获取每个大科室里面下级子科室
        Map<String, List<Department>> deparmentMap =
                departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));

        //遍历map集合 deparmentMap
        for(Map.Entry<String,List<Department>> entry : deparmentMap.entrySet()) {
            //大科室编号
            String bigcode = entry.getKey();
            //大科室编号对应的全局数据
            List<Department> deparment1List = entry.getValue();

            //封装大科室
            DepartmentVo departmentVo1 = new DepartmentVo();
            departmentVo1.setDepcode(bigcode);
            departmentVo1.setDepname(deparment1List.get(0).getBigname());

            //封装小科室
            List<DepartmentVo> children = new ArrayList<>();
            for(Department department: deparment1List) {
                DepartmentVo departmentVo2 =  new DepartmentVo();
                departmentVo2.setDepcode(department.getDepcode());
                departmentVo2.setDepname(department.getDepname());
                //封装到list集合
                children.add(departmentVo2);
            }

            //把小科室list集合放到大科室children里面
            departmentVo1.setChildren(children);

            //放到最终result里面
            result.add(departmentVo1);
        }

        //返回
        return result;
    }

标签:value,String,中转,private,ApiModelProperty,json,科室,Department,对应
From: https://www.cnblogs.com/chillymint/p/17263440.html

相关文章

  • apollo配置json
    #json串原文[{"username":"李小刚","sex":"男"},{"username":"苗翠花","sex":"女"}]publicclassMyDTO{privateStringusername;privateStringsex;......
  • fastjson 常用方法
    publicstaticfinalObjectparse(Stringtext);//把JSON文本parse为JSONObject或者JSONArraypublicstaticfinalJSONObjectparseObject(Stringtext);//把JSON文......
  • Java 在代码中区分json和array
    publicstaticvoidmain(String[]args){Stringn="{\n"+""data":[\n"+"{\n"+""category":"设计资质",\n"+""certNameL......
  • JSON & import assertions All In One
    JSON&importassertionsAllInOneerror//constpackageInfo=require("./package.json");import*aspkgfrom"./package.json";console.log(`pkg`,pkg);......
  • scala的json项目
    有下面的scala代码packagejobsimportorg.json4s._importorg.json4s.jackson.JsonMethods._objectTest{defmain(args:Array[String]):Unit={case......
  • Linux环境下使用jsoncpp
    目录1.下载jsoncpp2.生成静态库libjsoncpp.a3.复制相关文件至/usr/local下(方便编程)4.CMakeList.txt编写(需要新增的)1.下载jsoncpp-->https://github.com/open-so......
  • go语言学习-json和xml
    JSONjson是完全独立于语言的文本格式,是k-v的形式name:zs应用场景:前后端交互,系统间数据交互json使用go语言内置的encoding/json标准库编码json使用json.Marshal()函数可以......
  • Angular 应用里 ng-package.json 文件的作用是什么?
    如下图所示:{"$schema":"../../node_modules/ng-packagr/ng-package.schema.json","dest":"../../dist/core","lib":{"entryFile":"./public_api.ts"......
  • npm package.json exports filed All In One
    npmpackage.jsonexportsfiledAllInOnehttps://docs.npmjs.com/files/package.jsonhttps://docs.npmjs.com/cli/v9/configuring-npm/package-json/#mainhttps://do......
  • [FastAPI-24]jsonable_encoder 序列化
    importtypingimportjsonfromfastapiimportFastAPI,Responsefromfastapi.encodersimportjsonable_encoderfromdatetimeimportdatetimefrompydanticimpor......