将泛型类型的Json转换为特定的Json格式

我正在使用 Java 8,我尝试使用“Collectors.groupingBy”,但没有获得所需的格式


public class ConsigneeLoadDetails {


    private Integer tripID;

    private String consignor;

    private String consignee;

    private String city;

    private Double distanceToNext;

    private Double load;

    private Integer cap;

    private Integer vehicleCapacity;

    private Double cLoad;

    private Integer truckUtilisation;

    private Double latitude;

    private Double longitude;  


    // getter , setter of all parameters 

}




List<ConsigneeLoadDetails> listConsigneeLoadDetails = new ArrayList();


// loop to add ConsigneeLoadDetails


listConsigneeLoadDetails.add(consigneeLoadDetails);

我在 API 响应中低于 Json

https://img1.mukewang.com/64d356a1000190f103560809.jpg

我想要以下格式的回复

https://img1.mukewang.com/64d356b300017dc604000745.jpg


慕仙森
浏览 132回答 2
2回答

Qyouu

你为什么不重新安排你的 POJO 类,像这样public class ConsigneeLoadDetails {&nbsp; &nbsp; private Integer tripID;&nbsp; &nbsp; private String consignor;&nbsp; &nbsp; private Integer vehicleCapacity;&nbsp; &nbsp; private Double cLoad;&nbsp; &nbsp; private Integer truckUtilisation;&nbsp; &nbsp; private List<consigneeDetails> consigneeDetails&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; private Double latitude;&nbsp; &nbsp; private Double longitude;&nbsp;&nbsp;&nbsp; &nbsp; // getter , setter of all parameters&nbsp;}public class ConsigneeDetails {&nbsp; &nbsp; private String consignee;&nbsp; &nbsp; private String city;&nbsp; &nbsp; private Double distanceToNext;&nbsp; &nbsp; private Double load;&nbsp; &nbsp; private Integer cap;&nbsp; &nbsp; // getter , setter of all parameters&nbsp;}

30秒到达战场

Map<Integer, List<ConsigneeLoadDetails>> collect =// group the object by id&nbsp; &nbsp;listConsigneeLoadDetails.stream().collect(Collectors.groupingBy(consigneeLoadDetails -> consigneeLoadDetails.tripID));&nbsp; &nbsp; &nbsp; &nbsp; List<ConsigneeLoadDetailsDto> dtoList = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; collect.forEach((integer, consigneeLoadDetails) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<ConsigneeLoadDetails> list = consigneeLoadDetails;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Optional.ofNullable(list).isPresent() && !list.isEmpty()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ConsigneeLoadDetailsDto dtos = new ConsigneeLoadDetailsDto();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dtos.setTripID(integer);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dtos.setConsignor(list.stream().findFirst().get().getConsignor());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dtos.setVehicleCapacity(list.stream().findFirst().get().getVehicleCapacity());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dtos.setCLoad(list.stream().findFirst().get().getCLoad());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dtos.setTruckUtilisation(list.stream().findFirst().get().getTruckUtilisation());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<ConsigneeDetails> consigneeDetails = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.forEach(next -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ConsigneeDetails con = new ConsigneeDetails();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; con.setCap(next.getCap());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; con.setCity(next.getCity());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; con.setConsignee(next.getConsignee());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; con.setDistanceToNext(next.getDistanceToNext());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; con.setLoad(next.getLoad());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; consigneeDetails.add(con);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dtos.setConsigneeDetails(consigneeDetails);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dtoList.add(dtos);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });// Your result will be store in&nbsp;// dtoList&nbsp; &nbsp; }}@Getter@Setter@ToStringclass ConsigneeLoadDetailsDto {&nbsp; &nbsp; private Integer tripID;&nbsp; &nbsp; private String consignor;&nbsp; &nbsp; private Integer vehicleCapacity;&nbsp; &nbsp; private Double cLoad;&nbsp; &nbsp; private Integer truckUtilisation;&nbsp; &nbsp; private List<ConsigneeDetails> consigneeDetails;}@Getter@Setter@ToStringclass ConsigneeDetails {&nbsp; &nbsp; private String consignee;&nbsp; &nbsp; private String city;&nbsp; &nbsp; private Double distanceToNext;&nbsp; &nbsp; private Double load;&nbsp; &nbsp; private Integer cap;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java