简体   繁体   English

如何将String转换为array / object / LIst

[英]How to convert String to array/object/LIst

String str = "[[28.667949,77.287067,232,0,0.8,5],[28.667949,77.287067,232,0,0.8,5]]";

I have a String, and want to convert it into any type either in Array,List or Object except String. 我有一个String,并且想要将其转换为Array,List或Object中除String外的任何类型。

Expected Output : 预期产量:

 List/Array/Object =
 [[28.667949,77.287067,232,0,0.8,5],[28.667949,77.287067,232,0,0.8,5]]

If groups of data is important, here's an algo: 如果数据组很重要,这是一种算法:

1. create new List<List<String>> strList
2. String [] strs= str.split("],")
3. loop each str in strs
  3.1 replace all existence of "[" and "]" of str
  3.2 strList.add(Arrays.asList(str.split(",")))

Hope this helps! 希望这可以帮助!

你必须做

List ls = Arrays.asList(str.replaceAll("\\[", "").replaceAll("\\]", "").split(","));

There is work around for this 有解决方法

1.) First replace all '[' and ']' with "" 1.)首先用“”替换所有“ [”和“]”

str = str.replaceAll("[","");
str = str.replaceAll("]","");

2.)Then u can split the string with "," 2.)然后,您可以使用“,”分割字符串

which gives u an array.
String[] split = str.split(",");

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

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