I am working in Java to build JSON objects and want to send them into a JSON array all because of unsorted data into my database. I am not able to map the data into right format. I think I am lacking in logic.
Below is the image of my database table, which is in MySQL:
What I want to create is a JSON format for this table which looks like this:
[
{
"1": "450",
"2": "495",
"OUTLET": "TOTTAL2",
"BILLDATE": "",
"TOTAL": "945"
},
{
"1": "10",
"2": "15",
"OUTLET": "Ol1",
"BILLDATE": "08-21-2018",
"TOTAL": "25"
},
{
"1": "20",
"2": "25",
"OUTLET": "ol1",
"BILLDATE": "08-22-2018",
"TOTAL": "45"
},
{
"1": "30",
"2": "35",
"OUTLET": "ol1",
"BILLDATE": "08-23-2018",
"TOTAL": "65"
},
{
"1": "40",
"2": "45",
"OUTLET": "ol2",
"BILLDATE": "08-21-2018",
"TOTAL": "85"
},
{
"1": "50",
"2": "55",
"OUTLET": "ol2",
"BILLDATE": "08-22-18",
"TOTAL": "105"
},
{
"1": "60",
"2": "65",
"OUTLET": "ol2",
"BILLDATE": "08-23-2018",
"TOTAL": "125"
},
{
"1": "70",
"2": "75",
"OUTLET": "ol3",
"BILLDATE": "08-21-2018",
"TOTAL": "145"
},
{
"1": "80",
"2": "85",
"OUTLET": "ol3",
"BILLDATE": "08-22-2018",
"TOTAL": "165"
},
{
"1": "90",
"2": "95",
"OUTLET": "ol3",
"BILLDATE": "08-23-201818",
"TOTAL": "185"
}
]
The above JSON is my desired output. But I am not able to map the data in loop or I am not getting the logic. I just want to create a JSON format for the html table I have also provided the json that I want.
The place where I am lagging is in coding and thinking of logic that how could I loop the data to get desired format:
And this is the table which i want to make
I have knowledge of GSON in java to parse json (send json in Java) so I can do that the only thing I want is to like how can I code forth.
Here is my Java code code by which I am getting the first header
String TotalAmountWithDateSql = "quwry1";
// System.out.println("TotalAmountWithDateSql"+TotalAmountWithDateSql);
String GrandTotalSql = "query2";
// System.out.println("grandTotal"+GrandTotalSql);
try {
con = DBConnection.createConnection();
statement = con.createStatement();
ResultSet resultSet = statement.executeQuery(GrandTotalSql);
while (resultSet.next()) {
map.put("OUTLET/HOURS", " ALL");
GrandTotal = resultSet.getLong("TOTAL");
map.put("TOTAL", GrandTotal);
resultSet = statement.executeQuery(TotalAmountWithDateSql);
while (resultSet.next())
{
BillTime = resultSet.getString("TIME");
NetAmtWithTime = resultSet.getLong("AMOUNT");
map.put(BillTime, NetAmtWithTime);
}
list.add(map);
str = gson.toJson(list);
}
System.out.println("value " + str);
response.setContentType("application/json");
response.getWriter().write(str);
from this Java code I am getting
Now I want to call data below this like the JSON I have uploaded, but this is only giving me the first header:



SELECTstatement, retrieve data using JDBC, JPA, or other means, manipulate the data into the desired form, generating JSON from some in-memory data structure. In short, as question currently stands, it is way too broad.