简体   繁体   中英

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" }

I'm trying to convert this JSON object into List of Strings

It should look like this,

List[ "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()]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM