我正在用Java来构建JSON对象,并希望将它们发送到JSON数组中,因为所有未排序的数据都进入了数据库。我无法将数据映射为正确的格式。我认为我缺乏逻辑。
下面是我的数据库表的图像,它在MySQL中:
我要为此表创建一个JSON格式,如下所示:
[
{
"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"
}
]
上面的JSON是我想要的输出。但是我无法循环映射数据,或者我没有逻辑。我只想为html表创建JSON格式,我也提供了所需的json。
我滞后的地方是编码和逻辑思考,我该如何循环数据以获得所需的格式:
这是我要制作的桌子
我了解Java中的GSON来解析json(用Java发送json),所以我能做的唯一的事情就是喜欢如何编码。
这是我获得第一个标头的Java代码
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);
我从此Java代码中获得
现在,我想调用下面的数据,例如我上传的JSON,但这只是给我第一个标头:
答案 0 :(得分:4)
让我们退后一步,尝试在SQL中做一切。 MySQL可以使用WITH ROLLUP
(类似于标准SQL GROUPING SETS
)来计算总计,您不需要两次往返。另外,MySQL在某种程度上支持标准SQL/JSON,因此您无需使用Java进行任何格式化。
SELECT
CASE
-- The label of the grand total
WHEN GROUPING(outlet) = 1 THEN 'TOTAL2'
-- Prevent printing the outlet on rows 2+ per outlet group
WHEN row_number() OVER (PARTITION BY outlet ORDER BY billdate) = 1 THEN outlet
END AS outlet,
billdate,
SUM(netAmount) AS `Total1/Ho...`,
-- This is just a silly assumption based on your data.
-- Your actual formula might be more involved
SUM(CASE WHEN mod(hours, 2) = 1 THEN netAmount END) AS `1`,
SUM(CASE WHEN mod(hours, 2) = 0 THEN netAmount END) AS `2`
-- Possibly more?
FROM t
GROUP BY outlet, billdate WITH ROLLUP
-- Get only the grand total or ordinary groupings
HAVING GROUPING(outlet) = 1 OR GROUPING(billdate) = 0
-- Possibly add ORDER BY here to get the desired ordering if that's required
为简单起见,我不会重写上面的整个查询,而是将其放在CTE中,这是构建SQL的便捷工具:
WITH
t AS (
-- Copy paste previous query here
)
SELECT
json_arrayagg(json_object(
'1', t.`1`,
'2', t.`2`,
'OUTLET', t.outlet,
'BILLDATE', t.billdate,
'TOTAL', t.total
))
FROM t
请注意,您还可以使用第三方库,例如jOOQ,which can export data as JSON out of the box或which supports SQL/JSON functions(免责声明,我为jOOQ背后的公司工作)。
答案 1 :(得分:1)
这确实很简单。您需要获取所有记录并创建一个包含地图对象的列表,其中每个地图对象将包含您想要的那五个键值对。
以下是您需要编写的那种代码,
public static void main(String[] args) throws IOException {
createJson();
}
public static List<Map<String, String>> getMapList(ResultSet rs) throws SQLException {
List<Map<String, String>> mapList = new ArrayList<Map<String,String>>();
while(rs.next()) {
Map<String, String> map = new LinkedHashMap<String, String>();
map.put("1", rs.getString("col1"));
map.put("2", rs.getString("col2"));
map.put("OUTLET", rs.getString("outlet"));
map.put("BILLDATE", rs.getString("billdate"));
map.put("TOTAL", rs.getString("total"));
mapList.add(map);
}
return mapList;
}
public static void createJson() {
List<Map<String, String>> mapList = new ArrayList<Map<String,String>>();
Map<String, String> map1 = new LinkedHashMap<String, String>();
map1.put("1", "50");
map1.put("2", "55");
map1.put("OUTLET", "ol2");
map1.put("BILLDATE", "08-22-18");
map1.put("TOTAL", "105");
Map<String, String> map2 = new LinkedHashMap<String, String>();
map2.put("1", "60");
map2.put("2", "65");
map2.put("OUTLET", "ol3");
map2.put("BILLDATE", "08-23-18");
map2.put("TOTAL", "125");
mapList.add(map1);
mapList.add(map2);
String json = new Gson().toJson(mapList);
System.out.println(json);
}
希望这会有所帮助。