有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

json需要建议如何迭代Java列表<Map<String,String>>

我需要关于如何最好地迭代List<Map<String, String>>对象以获得以下结果的建议:

该对象保存从sql数据库获取的数据。每个映射条目描述一列返回的数据:

0 =
    0 =
      key = "ColumnA"
      value = "1"
    1 =
      key = "ColumnB"
      value = "2"
    2 =
      key = "ColumnC"
      value = "3"
1 =
    0 =
      key = "ColumnA"
      value = "1"
    1 =
      key = "ColumnB"
      value = "2"
    2 =
      key = "ColumnC"
      value = "3"

实际数据示例如下所示:

0 =
    0 =
      key = "Itemtype"
      value = "1"
    1 =
      key = "Itemdate"
      value = "01.01.2018"
    2 =
      key = "Subitem"
      value = "A"
    3 =
      key = "Subitemdetail"
      value = "A"
    4 =
      key = "Subitemdetail2"
      value = "A"
1 =
    0 =
      key = "Itemtype"
      value = "1"
    1 =
      key = "Itemdate"
      value = "01.01.2018"
    2 =
      key = "Subitem"
      value = "B"
    3 =
      key = "Subitemdetail"
      value = "B"
    4 =
      key = "Subitemdetail2"
      value = "B"
2 =
    0 =
      key = "Itemtype"
      value = "2"
    1 =
      key = "Itemdate"
      value = "01.01.2018"
    2 =
      key = "Subitem"
      value = "A"
    3 =
      key = "Subitemdetail"
      value = "A"
    4 =
      key = "Subitemdetail2"
      value = "A"
3 =
    0 =
      key = "Itemtype"
      value = "2"
    1 =
      key = "Itemdate"
      value = "01.01.2018"
    2 =
      key = "Subitem"
      value = "B"
    3 =
      key = "Subitemdetail"
      value = "B"
    4 =
      key = "Subitemdetail2"
      value = "B"

目前,数据通过代码段以类似于JSON的格式返回:

  JSONArray resultSet = new JSONArray();   

  for (Map<String, String> res : data) {
      JSONObject resObject = new JSONObject();

      for (Entry<String, String> subres : res.entrySet()) {
         resObject.put(subres.getKey(), subres.getValue());
      }

     resultSet.put(resObject);
  }

上面的代码段返回以下JSON:

{
"res":[
    {
        "Itemtype": "1",
        "Itemdate": "01.01.2018",
        "Subitem": "A",
        "Subitemdetail": "A",
        "Subitemdetail2": "A"
    },
    {
        "Itemtype": "1",
        "Itemdate": "01.01.2018",
        "Subitem": "B",
        "Subitemdetail": "B",
        "Subitemdetail2": "B"
    },
    {
        "Itemtype": "2",
        "Itemdate": "01.01.2018",
        "Subitem": "A",
        "Subitemdetail": "A",
        "Subitemdetail2": "A"
    },
    {
        "Itemtype": "2",
        "Itemdate": "01.01.2018",
        "Subitem": "B",
        "Subitemdetail": "B",
        "Subitemdetail2": "B"
    }
]}

期望的结果:

但是,我现在想根据Itemtype值对JSON进行分组。预期结果如下:

{
"result":[
{
    "Itemtype": "1",
    "Itemdate": "01.01.2018",
    "Subitem": [
        {
            "Subitem": "A",
            "Subitemdetail": "A",
            "Subitemdetail2": "A"
        },
        {
            "Subitem": "B",
            "Subitemdetail": "B",
            "Subitemdetail2": "B"
        }
    ]
},
{
    "Itemtype": "2",
    "Itemdate": "01.01.2018",
    "Subitem": [
        {
            "Subitem": "A",
            "Subitemdetail": "A",
            "Subitemdetail2": "A"
        },
        {
            "Subitem": "B",
            "Subitemdetail": "B",
            "Subitemdetail2": "B"
        }
    ]
}
]}

我正试图想出一种方法来最好地遍历List<Map<String, String>>对象。你能给我一个建议吗

因为我目前只能想到非常难看的解决方案,例如首先遍历对象作为一个整体,并存储一个项目类型列表及其位置,这意味着上面的示例: 第1项:0,1和第2项:1,2。 然后,我会浏览这个列表,为自己构建JSON。但也许你能给我一个更好的建议?或者有没有一个Java8流可以更好地解决这个问题

提前谢谢


共 (1) 个答案

  1. # 1 楼答案

    您可以使用groupingBy流收集器获得一个映射,然后通过reduce将每个条目转换为所需的最终结构

    差不多

    // Group into Itemtype -> objects Map
    Map<String, List<JSONObject>> grouped = results.stream().collect(groupingBy(obj -> obj.getString("Itemtype"))
    
    // Reduce entries into single JSON array where the Itemtype is a top level property and the entries are under Subitem
    grouped.entries().stream().reduce(result, entry-> {
        JSONObject obj = new JSONObject();
        obj.putString("Itemtype", entry.getKey());
        obj.putObject("Subitem", entry.getValue());
    
        result.put(obj);
        return result;
    }, new JSONArray())
    

    这并不能完全满足你的需求,但我相信你可以解决剩下的问题