将对象数组转换为不同格式的数组

时间:2020-10-27 16:37:07

标签: java

我在Java中有一组对象,这些对象是从SQL服务器返回的,我想将它们转换并将其发布到React Application。 目标是将2d表填充为react。我不想转换为客户端。 我希望数据可以随时提供。

这是我要转换的对象数组:

[
  {
    "tentId": 34,
    "timeslot": "2020-03-01T01:00:00"
  },
  {
    "tentId": 34,
    "timeslot": "2020-03-02T01:00:00"
  },
  {
    "tentId": 34,
    "timeslot": "2020-03-03T01:00:00"
  },
  {
    "tentId": 32,
    "timeslot": "2020-05-01T01:00:00"
  },
  {
    "tentId": 32,
    "timeslot": "2020-05-02T01:00:00"
  },
  {
    "tentId": 32,
    "timeslot": "2020-05-03T01:00:00"
  },
  {
    "tentId": 32,
    "timeslot": "2020-05-04T01:00:00"
  },
  {
    "tentId": 32,
    "timeslot": "2020-05-05T01:00:00"
  },
  {
    "tentId": 32,
    "timeslot": "2020-05-06T01:00:00"
  },
  {
    "tentId": 32,
    "timeslot": "2020-05-07T01:00:00"
  },
  {
    "tentId": 32,
    "timeslot": "2020-05-08T01:00:00"
  },
  {
    "tentId": 32,
    "timeslot": "2020-05-09T01:00:00"
  },
  {
    "tentId": 32,
    "timeslot": "2020-05-10T01:00:00"
  },
  {
    "tentId": 35,
    "timeslot": "2020-05-29T01:00:00"
  },
  {
    "tentId": 35,
    "timeslot": "2020-05-30T01:00:00"
  },
  {
    "tentId": 35,
    "timeslot": "2020-05-31T01:00:00"
  }
 ]

这是我想要的结果。

 [
  {
    "tentId": 34,[
    {"timeslot": "2020-03-01T01:00:00"},
    {"timeslot": "2020-03-02T01:00:00"},
    {"timeslot": "2020-03-03T01:00:00"}
    ]},
  {
    "tentId": 32,[
  { "timeslot": "2020-05-01T01:00:00"},
  { "timeslot": "2020-05-02T01:00:00"},
  {"timeslot": "2020-05-03T01:00:00"},
  { "timeslot": "2020-05-04T01:00:00"},
  {"timeslot": "2020-05-05T01:00:00"},
  {"timeslot": "2020-05-06T01:00:00"},
  {"timeslot": "2020-05-07T01:00:00"},
  {"timeslot": "2020-05-08T01:00:00"},
  {"timeslot": "2020-05-09T01:00:00"},
  {"timeslot": "2020-05-10T01:00:00"},
  {"timeslot": "2020-05-29T01:00:00"}
  ]},
  {
    "tentId": 35,[
  { "timeslot": "2020-05-30T01:00:00"},
  { "timeslot": "2020-05-31T01:00:00"}
  ]}
 ]

这些是我的dto对象

public class FinalTentBookingListDTO {
    Long tentId;

    List<LocalDateTime> timeslots;

    public Long getTentId() {
        return tentId;
    }

    public void setTentId(Long tentId) {
        this.tentId = tentId;
    }

    public List<LocalDateTime> getTimeslots() {
        return timeslots;
    }

    public void setTimeslots(List<LocalDateTime> timeslots) {
        this.timeslots = timeslots;
    }


    @Override
    public String toString() {
        return "FinalTentBookingListDTO{" +
                "tentId=" + tentId +
                ", timeslots=" + timeslots +
                '}';
    }
}


public class TentWithAllBookedTimeslotsDTO {
    Long tentId;
    LocalDateTime timeslot;

    public Long getTentId() {
        return tentId;
    }

    public void setTentId(Long tentId) {
        this.tentId = tentId;
    }

    public LocalDateTime getTimeslot() {
        return timeslot;
    }

    public void setTimeslot(LocalDateTime timeslot) {
        this.timeslot = timeslot;
    }

    @Override
    public String toString() {
        return "TentWithAllBookedTimeslotsDTO{" +
                "tentID=" + tentId +
                ", timeslot=" + timeslot +
                '}';
    }
}

我尝试了许多方法,包括平面图,但没有成功。 非常感谢。

3 个答案:

答案 0 :(得分:1)

您可以使用Java Stream API来实现。解决问题的示例:

        final Map<Long, List<TentWithAllBookedTimeslotsDTO>> map = tents.stream().collect(Collectors.groupingBy(TentWithAllBookedTimeslotsDTO::getTentId));
        final List<FinalTentBookingListDTO> dtos = map.keySet().stream()
                .map((final Long tentId) -> {
                    final List<LocalDateTime> dateTimes = map.get(tentId).stream().map(TentWithAllBookedTimeslotsDTO::getTimeslot).collect(Collectors.toList());
                    return new FinalTentBookingListDTO(tentId, dateTimes);
                }).collect(Collectors.toList());

在您的代码中没有构造函数,但我认为您可以自己编写代码。

答案 1 :(得分:0)

我的回答是,如果DB返回JSON内容作为响应,则可以直接从数据库返回数据。 否则,您仅需要将db格式转换为DTO,然后使用jackson或gson可以将其转换为json格式。

只需将响应内容类型设置为“ application / json”,如果是Json,则发送db conent,否则进行转换,然后使用libs将dtos转换为json。

答案 2 :(得分:0)

假设您将必需的构造函数添加到DTO中,则可以使用一行在Java 8中进行此转换

List<FinalTentBookingListDTO> finalList = tentWithAllBookedTimeslotsDTOOList.stream()
                                                        .collect(groupingBy(TentWithAllBookedTimeslotsDTO::getTentId,
                                                                mapping(TentWithAllBookedTimeslotsDTO::getTimeslot, Collectors.toList())))
                                                        .entrySet()
                                                        .stream()
                                                        .map(entry -> new FinalTentBookingListDTO(entry.getKey(), entry.getValue()))
                                                        .collect(Collectors.toList());
相关问题