I have an array of objects in java that I return from a SQL server and I want to convert them and post them to a react Application. The goal is to fill a 2d table into react. I do not want to make the conversion into client. I want the data to be served ready.
Here is the array of objects I want to convert :
[
{
"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"
}
]
This is the results I want.
[
{
"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"}
]}
]
these are my dto objects
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 +
'}';
}
}
I have tried with many methods including flatmap but I had no success. Thank you very much.
List<TentWithAllBookedTimeslotsDTO>into a JSON String with a given format?tentId? Did you research hpow to group data using Java Streams?