I have a project in SpringBoot and I am looking to create a JSON object to send to my FrontEnd project, I am looking to create a JSON like this:
{ "result": { "data": [ { "month": "January", "APERTURAS": 0 }, { "month": "February", "APERTURAS": 0 }, { "month": "March", "APERTURAS": 0 }, { "month": "April", "APERTURAS": 0 }, { "month": "May", "APERTURAS": 0 }, { "month": "June", "APERTURAS": 0 }, { "month": "July", "APERTURAS": 0 }, { "month": "August", "APERTURAS": 3 }, { "month": "September", "APERTURAS": 11 }, { "month": "October", "APERTURAS": 3 }, { "month": "November", "APERTURAS": 0 }, { "month": "December", "APERTURAS": 0 } ] } }
in this one, amounts are being totaled per month, I am making a query and with this I obtain the months in which there is information, I need to create the json with the information of all the months of the year, but if for example the query does not return the amount of a specific month or months, I want to indicate this in zero.
with my code I am getting the following JSON:
{ "result": { "data": [ {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, { "APERTURAS": 0, "month": "August" }, {}, {}, {}, { "APERTURAS": 0, "month": "September" }, {}, {}, {}, { "APERTURAS": 3, "month": "October" }, {}, {}, {}, {}, {}, {} ] }}
This is my code:
private static final String[] MESES_COMPLETO = {"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"};
List<ReporteTipoActoMesInterfaceDTO> resultadoAperturas = repositorioReporte.reporteTipoActoAperturaMes(String.valueOf(year));
List<Object> reporteList = new ArrayList<Object>();
JSONObject reporteJson = new JSONObject();
for (int j = 0; j < MESES_COMPLETO.length; j++) {
JSONObject reporteJson = new JSONObject();
for (int k = 0; k < resultadoAperturas.size(); k++) {
JSONObject reporteJson2 = new JSONObject();
String mes = resultadoAperturas.get(k).getmes().replaceAll("\\s+","");
if (!MESES_COMPLETO[j].equals(mes)) {
reporteJson.put("month", MESES_COMPLETO[j]);
reporteJson.put("APERTURAS", 0);
reporteList.add(reporteJson2);
}
else {
if (Arrays.asList(MESES_COMPLETO[j]).contains(mes)) {
reporteJson.put("month", mes);
reporteJson.put("APERTURAS", resultadoAperturas.get(k).getexpedientes());
reporteList.add(reporteJson);
}
}
}
}
JSONObject jsonData = new JSONObject();
jsonData.put("data", reporteList);
jsonResponse = new JSONObject().put("result", jsonData).toString();
This is DTO
public interface ReporteTipoActoMesInterfaceDTO {
Long getexpedientes();
String getmes();
}
This is the result of the query
EXPEDIENTES MES
3 August
11 September
3 October
What am i doing wrong?