将 List<List<TextValue>> 转换为 List<Map<String, String>>

豪瑟斯

我有这个输出 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>>

WJS

这是嵌套的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);
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将List <Map <String,List <String >>>转换为String [] []

Java将List <String>转换为Map <String,String>

将Map <String,Object>转换为Map <String,List <Object >>

Java-8流:将List <{String,List <String>}>转换为Map <String,List <String >>

将 List<List<String>> 转换为 String[][]

将“ Map <String,List <String >>”强制转换为“ Map <String,List <String >>”

Java-8流:将Map <String,List <List <DataType >>>转换为Map <String,List <DataType >>

Kotlin FP:将List <String>转换为Map <String,Int>

将List <Map <String,Object >>转换为String [] []

将List <String>转换为Map <String,Integer>

使用Terraform将列表(map(list(map(string()))))转换为map(list(map(string))))

如何将List <Map <String,Object >>转换为List <Map <String,String >>

Scala - 将 List[((String, String), Double)] 转换为 List[String,Map[String,Double]]

将List <Integer>转换为List <String>

将List <dynamic>转换为List <string>

将 List<List<String>> 转换为 csv

将List <T>转换为List <string>

将List <List <object >>转换为List <List <string >>

如何将List <Map <?,?>>转换为List <Map <String,String >>?

将 byte[] 转换为 List<Map<String, Object>>

Java:如何将List <?>转换为Map <String,?>

将list <map <string,object >>转换为POJO类的对象

使用Dart将String转换为List / Map

将 Stream<QuerySnapshot<Map<String, dynamic>>> 转换为 List?

将 JSON 数据转换为 List<Map<String, dynamic>>

Java8:通过联接值将Map <String,List <String >>转换为Map <String,String>

Powershell将String []转换为List <String>

Groovy 2 List 转换为 Map(String,List<String>)

如何将List <NameValuePair>转换为List <Map.Entry <String,String >>?