如何将sql数据Map到java json对象和json数组?

nxagd54h  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(427)

我正在用java构建json对象,并希望将它们发送到一个json数组中,这一切都是因为我的数据库中存在未排序的数据。我无法将数据Map到正确的格式。我想我缺乏逻辑。
下面是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是我想要的输出。但我无法Map循环中的数据,或者我无法得到逻辑。我只想为html表创建一个json格式,我还提供了我想要的json。
我落后的地方在于编码和逻辑思维,即如何循环数据以获得所需的格式:
这就是我要做的table

我知道用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一样,但这只是给我第一个头:

r3i60tvu

r3i60tvu1#

让我们退一步,试着用sql做所有的事情。mysql可以使用 WITH ROLLUP (类似于标准sql) GROUPING SETS ),你不需要两次往返。另外,mysql支持(某种程度上)标准sql/json,因此您不必在java中进行任何格式化。

首先,让我们尝试在不生成json的情况下编写sql查询:

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

接下来,让我们生成json

为简单起见,我将不重写上面的整个查询,而是将其放在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这样的第三方库,它可以将数据导出为json开箱即用,或者支持sql/json函数(免责声明,我为jooq背后的公司工作)。

jtjikinw

jtjikinw2#

这确实很简单。您需要获取所有记录并创建一个包含Map对象的列表,其中每个Map对象将包含您想要的五个键值对。
下面是您需要编写的代码类型,

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);
}

希望这有帮助。

相关问题