转换列表<List<TextValue>> 为列表<Map<String, String>>

问题描述 投票:0回答:2

我有这个输出 JSON。它来自于这个类型

List<List<TextValue>>
TextValue 类是 String 文本,String 值。

"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 格式。 我相信这就是这个对象的设置。

List<Map<String, String>>

"data": [
    {
      "DescCode": "01",
      "DescName": "Description 1",
      "SecondCode": "01"
    },      
    {
      "DescCode":"02",
      "DescName":"Description 2",
      "SecondCode":"02"
    }
]

我的 JSON 是由我的 Spring REST 控制器自动创建的,所以我真的只需要 Java 对象,Json 只是供参考。 我这样说有错吗?

List<Map<String, String>>

java dictionary java-stream
2个回答
1
投票

这里是

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

这基本上会流式传输内部列表,然后流式传输每个列表以创建具有指定键和值的映射。然后将地图收集到一个列表中。

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}

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

0
投票

我想通了。加载我的

List<Map<String, String>>
数据类型就是答案。我以为我需要使用一些奇特的流处理,但这不是必需的。具有正确对象的嵌套循环让我到达那里。

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);
        NodeList nodeListInner = node.getChildNodes();
        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);
    }
© www.soinside.com 2019 - 2024. All rights reserved.