xieegai

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

一、需求

现有list:
[
    {
        "type":"呼吸系统",
        "illness":"肺气肿",
        "quotaName": "血压"
    },
    {
        "type":"呼吸系统",
        "illness":"肺气肿",
        "quotaName": "血常规"
    },
   {
        "type":"呼吸系统",
        "illness":"哮喘",
        "quotaName": "血常规"
    },
   {
        "type":"循环系统",
        "illness":"高血压",
        "quotaName": "心电图"
    },
   {
        "type":"循环系统",
        "illness":"高血压",
        "quotaName": "心电彩超"
    }
]
想得到的list:
[
    {
        "type":"呼吸系统",
        "illnessList":[
            {
               "name":"肺气肿", 
               "quotaList":[
                   {
                       "name":"血压"
                   },
                   {
                       "name":"血常规"
                   }
               ]
            },
            {
                "name":"哮喘",
                "quotaList":[
                    {
                        "name":"血常规"
                    }
                ]
            }
        ]
    },
    {
        "type":"循环系统",
        "illnessList":[
            {
                "name":"高血压",
                "quotaList":[
                    {
                        "name":"心电图"
                    },
                    {
                        "name":"心电彩超"
                    }
                ]
            }
        ]
    }
]

 

二、实现

  1)输入对象

package jsonarray;

/**
 * 输入对象
 */
public class FromDataBo {
    
    private String type;
    private String illness;
    private String quotaName;
    
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getIllness() {
        return illness;
    }
    public void setIllness(String illness) {
        this.illness = illness;
    }
    public String getQuotaName() {
        return quotaName;
    }
    public void setQuotaName(String quotaName) {
        this.quotaName = quotaName;
    }

}

  2)输出对象

package jsonarray;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 输出对象
 */
public class ToDataBo {
    
    private String type;
    private List<ToDataIllnessBo> illnessList;
    
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public List<ToDataIllnessBo> getIllnessList() {
        return illnessList;
    }
    public void setIllnessList(List<ToDataIllnessBo> illnessList) {
        this.illnessList = illnessList;
    }

    /**
     * map转化为List<ToDataBo>
     * @param map
     * @return
     */
    public static List<ToDataBo> createByMap(Map<String, Map<String, List<String>>> map){
        return map.entrySet().stream().map(ToDataBo::of).collect(Collectors.toList());
    }

    /**
     * 一个Map.Entry<String, Map<String, List<String>>>对应转化为一个ToDataBo
     * @param entry
     * @return
     */
    public static ToDataBo of(Map.Entry<String, Map<String, List<String>>> entry){
        ToDataBo dataBo = new ToDataBo();
        dataBo.setType(entry.getKey());
        dataBo.setIllnessList(entry.getValue().entrySet().stream().map(ToDataIllnessBo::of).collect(Collectors.toList()));
        return dataBo;
    }
    
    static class ToDataIllnessBo{
        private String name;
        private List<ToDataQuotaBo> quotaList;

        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }

        public List<ToDataQuotaBo> getQuotaList() {
            return quotaList;
        }
        public void setQuotaList(List<ToDataQuotaBo> quotaList) {
            this.quotaList = quotaList;
        }

        /**
         * 一个Map.Entry<String, List<String>>对应转化为一个ToDataIllnessBo
         * @param entry
         * @return
         */
        public static ToDataIllnessBo of(Map.Entry<String, List<String>> entry){
            ToDataIllnessBo dataIllnessBo = new ToDataIllnessBo();
            dataIllnessBo.setName(entry.getKey());
            dataIllnessBo.setQuotaList(entry.getValue().stream().map(ToDataQuotaBo::new).collect(Collectors.toList()));
            return dataIllnessBo;
        }
    }
    
    static class ToDataQuotaBo {
        private String name;

        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }

        public ToDataQuotaBo(String name) {
            this.name = name;
        }
        public ToDataQuotaBo() {}
    }
}

  3)测试类

package jsonarray;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.alibaba.fastjson.JSONArray;

public class Test {
    
    public static void main(String[] args) {
        
        String from = "[\n" +
                "        {\n" +
                "            \"type\":\"呼吸系统\",\n" +
                "            \"illness\":\"肺气肿\",\n" +
                "            \"quotaName\": \"血压\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"type\":\"呼吸系统\",\n" +
                "            \"illness\":\"肺气肿\",\n" +
                "            \"quotaName\": \"血常规\"\n" +
                "        },\n" +
                "       {\n" +
                "            \"type\":\"呼吸系统\",\n" +
                "            \"illness\":\"哮喘\",\n" +
                "            \"quotaName\": \"血常规\"\n" +
                "        },\n" +
                "       {\n" +
                "            \"type\":\"循环系统\",\n" +
                "            \"illness\":\"高血压\",\n" +
                "            \"quotaName\": \"心电图\"\n" +
                "        },\n" +
                "       {\n" +
                "            \"type\":\"循环系统\",\n" +
                "            \"illness\":\"高血压\",\n" +
                "            \"quotaName\": \"心电彩超\"\n" +
                "        }\n" +
                "    ]";
        
        // 把输入的JSONArray字符串转化为FromDataBo集合
        List<FromDataBo> fromDataBos = JSONArray.parseArray(from, FromDataBo.class);
        // 归类
        Map<String, Map<String, List<String>>> collect = fromDataBos.stream().collect(
                                // 按照type分类
                                Collectors.groupingBy(FromDataBo::getType,
                                        // 按照type分类后,同一类的数据再按照illness分类
                                        Collectors.groupingBy(FromDataBo::getIllness,
                                                // 按照type分类,再按照illness分类后,同一类的数据取其中的QuotaName并转化为集合
                                                Collectors.mapping(FromDataBo::getQuotaName, Collectors.toList()))));
        // 归类后的map转化为输出对象ToDataBo集合
        List<ToDataBo> toDataBos = ToDataBo.createByMap(collect);
        
        // 我是输出对象
        JSONArray result = JSONArray.parseArray(JSONArray.toJSONString(toDataBos));
        System.out.println(result);
        
    }
}

 

posted on 2018-01-05 23:17  xieegai  阅读(664)  评论(0编辑  收藏  举报