Java 转换列表<;列表<;TextValue>&燃气轮机;列出<;地图<;字符串,字符串>&燃气轮机;

Java 转换列表<;列表<;TextValue>&燃气轮机;列出<;地图<;字符串,字符串>&燃气轮机;,java,dictionary,java-stream,Java,Dictionary,Java Stream,我有这个输出JSON。它来自于这种类型 列表 TextValue类是字符串文本,字符串值 "data": [ [ { "text": "DescCode", "value": "01" }, { "text": "DescName", "value": "

我有这个输出JSON。它来自于这种类型
列表
TextValue类是字符串文本,字符串值

"data": [
  [
    {
      "text": "DescCode",
      "value": "01"
    },
    {
      "text": "DescName",
      "value": "Description 1"
    },
    {
      "text": "SecondCode",
      "value": "01"
    }
  ],
  [
    {
      "text": "DescCode",
      "value": "02"
    },
    {
      "text": "DescName",
      "value": "Description 2"
    },
    {
      "text": "SecondCode",
      "value": "02"
    }
  ]
]
我想把它转换成这个JSON输出。 我相信这就是这个对象设置。
列表

我的JSON是由我的SpringREST控制器自动创建的,所以我真的只需要Java对象,JSON仅供参考。
我是不是有点不对劲<代码>列表

我想出来了。加载我的
列表
数据类型是最重要的。我想我需要使用一些奇特的流处理,但这不是必要的。带正确对象的嵌套循环使我达到了目的

List<Map<String,String>> test = new ArrayList<>();
    String xmlResponseStringDesc = httpUtility.DoHttpRequest(url);
    Document doc = null;
    doc = XmlHelper.convertStringToXMLDocument(xmlResponseStringDesc);
    NodeList nodes= doc.getElementsByTagName("DescData");
    Integer i=0;

    for(int x=0;x<nodes.getLength();x++){
        Node node =nodes.item(x);
        List<Map<String, String>> nextList = new ArrayList<>();
        NodeList nodeListInner = node.getChildNodes();
        String code=node.getNodeName();
        Map<String,String> map = new HashMap<>();
        for(int y=0;y<nodeListInner.getLength();y++){
            Node nodeInner =nodeListInner.item(y);
            if(nodeInner.getTextContent().trim().equals("")){
                continue;
            }
            map.put(nodeInner.getNodeName(),nodeInner.getTextContent());
          }
        test.add(map);
    }
List test=newarraylist();
字符串xmlResponseStringDesc=httpUtility.DoHttpRequest(url);
单据单据=空;
doc=XmlHelper.convertStringToXMLDocument(xmlResponseStringDesc);
NodeList nodes=doc.getElementsByTagName(“DescData”);
整数i=0;

对于(int x=0;x,这里是
TextValue
对象的嵌套列表

List<List<TextValue>> list = List.of(
        List.of(new TextValue("DescCode", "01"),
                new TextValue("DescName", "Description 1"),
                new TextValue("SecondCode", "01")),
        List.of(new TextValue("DescCode", "02"),
                new TextValue("DescName", "Description 2"),
                new TextValue("SecondCode", "02")));
TextValue类

class TextValue {
    String text;
    String value;
    
    public TextValue(String text, String value) {
        this.text = text;
        this.value = value;
    }
    
    public String getText() {
        return text;
    }
    
    public String getValue() {
        return value;
    }
    
    @Override
    public String toString() {
        return String.format("%s,  %s", text, value);
    }
}

您好,您可以使用stream
map
方法将内部
列表
转换为
map
。或者从JSON解析可能会有所帮助
List<Map<String,String>> listOfMaps = list.stream()
       .map(lst->lst.stream()
        .collect(Collectors.toMap(TextValue::getText, TextValue::getValue)))
        .collect(Collectors.toList());

listOfMaps.forEach(System.out::println);
{DescName=Description 1, DescCode=01, SecondCode=01}
{DescName=Description 2, DescCode=02, SecondCode=02}

class TextValue {
    String text;
    String value;
    
    public TextValue(String text, String value) {
        this.text = text;
        this.value = value;
    }
    
    public String getText() {
        return text;
    }
    
    public String getValue() {
        return value;
    }
    
    @Override
    public String toString() {
        return String.format("%s,  %s", text, value);
    }
}