Java converts a String that looks like an array to an array of strings or a 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 completed in ", as is String. So the integer and logical elements are in this line, and those that look like "944" are also in String, String in String, if you do.

How do I take this and make it an array of Java strings or ArrayList strings?

+4
source share
2 answers

I solved this with Gson.

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

Trim the head and tail of the non-data, then split:

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

Expected assets that separate a comma are not included in a pair of quotes.

+1
source

Source: https://habr.com/ru/post/1569005/


All Articles