[英]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.