简体   繁体   English

如何将对象列表转换为字符串列表

[英]How to convert List of Object into List of Strings

I'm trying to Convert List of Object into List of Strings 我正在尝试将对象列表转换为字符串列表

My List of Custom Object is like this 我的自定义对象列表是这样的

convertedData { rawMaterialId: "3411", batchNumber: "166,465,963,962,785", location: "hhh,ooo,hhh,uio,pop", quantity: "900,302,560,650,989" } convertData {rawMaterialId:“ 3411”,batchNumber:“ 166,465,963,962,785”,位置:“ hhh,ooo,hhh,uio,pop”,数量:“ 900,302,560,650,989”}

I'm trying to convert this JSON object into List of Strings 我正在尝试将此JSON对象转换为字符串列表

It should look like this, 看起来应该像这样

List[ "3411" ,"166" ,"465" ,"963" ,"hhh","ooo","pop","900","302","560"] 列表[“ 3411”,“ 166”,“ 465”,“ 963”,“ hhh”,“ ooo”,“ pop”,“ 900”,“ 302”,“ 560”]

I tried with below code 我尝试了以下代码

String[] array = new String[convertedData.size()];
    int index = 0;
    for (Object value : convertedData) {
      array[index] = (String) value;
      index++;
    }

Any suggestions and correction welcomed.Thanks in advance. 欢迎提出任何建议和指正。谢谢。

You should split the converted data. 您应该拆分转换后的数据。

List<String> list = new ArrayList<>();
for (Object value : convertedData) {
    if (value instanceof String) {
        String str = (String) value;
        list.addAll(Arrays.asList(str.split(",")));
    }
}
String[] array = list.toArray(new String[list.size()]);
    List<String> list = new ArrayList<>();

    for (Object value : convertedData) {
        String[] wordList = ((String) value).split(",");
        for (String val : wordList) {
            list.add(val);
        }
    }

    String[] stringArray = list.toArray(new String[list.size()]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将字符串和对象的映射转换为字符串和列表的映射 - How can i convert Map of Strings and Object to a Map of Strings and List 如何将字符串列表转换为LinkedHashMap? - How to convert a list of Strings to a LinkedHashMap? 如何将字符串列表转换为对象列表? - How to convert a list of strings into a list of objects? 如何将json对象列表转换为包含每个对象中某些字段的字符串pojo列表? - How to convert list of json objects to pojo list of strings that contains some field from each object? 如何转换列表<list<object> &gt; 列出<list<string> &gt; 在 Java 中? </list<string></list<object> - How to convert the List<List<Object>> to List<List<String>> in Java? 如何转换列表<list<string> &gt; 进入列表<list<object> &gt;? </list<object></list<string> - How to convert List<List<String>> into List<List<Object>>? 如何转换清单 <String> Java中将字符串转换为BufferedImage - How to convert List<String> of Strings to BufferedImage in Java 如何将歌曲列表转换为字符串流? - How to convert a list of Songs into stream of Strings? 如何将枚举列表转换为字符串数组? - How to convert list of enums into array of strings? 如何将字符串列表转换为带有流的地图 - How to convert a List of strings into a Map with Streams
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM