简体   繁体   English

Java将看起来像Array的String转换为String Array或String ArrayList

[英]Java convert String that looks like an Array into a String Array or String ArrayList

I have a String that looks like an Array: 我有一个看起来像一个数组的字符串:

["944", "The name", "Hi, hi", 1, 6, 0, false, "the date"]

NOTE: The above is wrapped in " , like a String would be. So the integers and boolean are in this String and those like "944" are also in the String, a String in a String if you will. 注意:上面的内容包含在" ,就像字符串一样。所以整数和布尔值都在这个字符串中,那些像”944“的字符串也在字符串中,如果你愿意的话,字符串中的字符串。

How do I take that and make it a Java String Array or ArrayList of Strings? 我如何把它作为字符串的Java字符串数组或ArrayList?

I solved it using Gson. 我用Gson解决了它。

Type listType = new TypeToken<List<String>>() {}.getType();
List<String> postData = new Gson().fromJson(stringThatLooksLikeArray, listType);

Trim the head and tail of non-data then split: 修剪非数据的头部和尾部然后拆分:

String[] parts = str.replaceAll("^\\[|\\]$", "").split(",(?=(([^\"]*\"){2})*[^\"]*$)");

The look ahead assets that the comma being split on is not within a quote pair. 分割开的逗号的前瞻资产不在引号对中。

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

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