需要关于迭代Java列表的建议<;地图<;字符串,字符串>&燃气轮机;

需要关于迭代Java列表的建议<;地图<;字符串,字符串>&燃气轮机;,java,json,java-8,iteration,Java,Json,Java 8,Iteration,我需要关于如何最好地迭代列表对象以获得以下结果的建议: 该对象保存从sql数据库获取的数据。每个映射条目描述一列返回的数据: 0 = 0 = key = "ColumnA" value = "1" 1 = key = "ColumnB" value = "2" 2 = key = "ColumnC" value = "3" 1 = 0 = key = "ColumnA"

我需要关于如何最好地迭代
列表
对象以获得以下结果的建议:

该对象保存从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"
{
"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"
        }
    ]
}
]}
目前,通过代码段以类似于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);
  }
期望的结果:

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

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"
{
"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"
        }
    ]
}
]}
我正试图想出一种方法来最好地遍历
列表
对象。你能给我一个建议吗

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


提前谢谢

您可以使用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())
//分组到Itemtype->objects映射
Map group=results.stream().collect(groupingBy(obj->obj.getString(“Itemtype”))
//将条目减少到单个JSON数组中,其中Itemtype是顶级属性,条目位于子项下
grouped.entries().stream().reduce(结果,条目->{
JSONObject obj=新的JSONObject();
putString(“Itemtype”,entry.getKey());
obj.putObject(“子项”,entry.getValue());
结果:put(obj);
返回结果;
},新的JSONArray())

这并不能完全满足您对属性的要求,但我相信您可以解决其余问题。

您提取数据时出错了。您从web服务器或从任何地方获取的结果是否为正确的JSON格式?我肯定可以帮助您,我只需要更多信息。发布从服务器获取的数据结果。如果是您将在某个时候对数据执行一些业务逻辑,最好不要将数据放在类中,否则它会变得混乱(如您所见)你不是真的在用Java编程。我可以在几个小时内发布数据结果。但是你会如何以不同的方式提取数据呢?关于将数据放入类:我也这么想,但目前数据没有做任何其他事情,所以现在这只是一个额外的步骤,我仍然需要一种方法来迭代数据不管怎么说:)谢谢你的提示,我正在调查groupingBy,这是向前迈出的一步。虽然我不能让你的代码片段工作,但我面临的第一个问题是:groupingBy(obj->obj.getString(“Itemtype”)-除非我将分组对象作为映射而不是映射来工作,否则这将不起作用。这是一个未经实际测试就编写的示例,以说明如何完成。你应该完成它。