将list< list< textvalue>>转换为list< map< string,string>>

bgibtngc  于 2021-07-12  发布在  Java
关注(0)|答案(2)|浏览(456)

我有这个输出。它来自这种类型 List<List<TextValue>> textvalue类是string text,string value。

"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>>` 
0aydgbwb

0aydgbwb1#

下面是嵌套的 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")));

这基本上是流式处理内部列表,然后流式处理每个列表,以创建具有指定键和值的Map。然后将Map收集到一个列表中。

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

rn0zuynd2#

我想出来了。正在加载我的 List<Map<String, String>> 数据类型是anwser。我想我需要使用一些花哨的流处理,但那不是必须的。嵌套循环和正确的对象把我带到了那里。

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

相关问题