0

I have the followings string from the DB "[["22-1-2017;10:00-19:00"],["22-1-2017;10:00-19:00"]]"

Is there an easy way to transform the string into an arrayList?

Thanks in advance

6
  • That depends on what your definition of the word "easy" is. If you have nested structure as you showed us, in general you'd want to use some sort of parser to read it in. Commented Mar 24, 2017 at 9:56
  • An ArrayList of what? Strings? Commented Mar 24, 2017 at 9:56
  • @OH GOD SPIDERS yes of strings like [["22-1-2017;10:00-19:00"],["22-1-2017;10:00-19:00"]] Commented Mar 24, 2017 at 10:01
  • 1
    you could use a json parser... Commented Mar 24, 2017 at 10:03
  • 2
    @jValls your example "of strings like [["22-1-2017;10:00-19:00"],["22-1-2017;10:00-19:00"]]" shows List of Lists of String, not List of String. Please clarify it. Commented Mar 24, 2017 at 10:14

3 Answers 3

1

How about using JSON parser like gson?

String jsonArray = "[[\"22-1-2017;10:00-19:00\"],[\"22-1-2017;10:00-19:00\"]]";

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

System.out.println(list);

Output: [[22-1-2017;10:00-19:00], [22-1-2017;10:00-19:00]]

Sign up to request clarification or add additional context in comments.

1 Comment

works perfect! Just what I need it simple and effective. I also imported the followings packages for Type and TypeToken: import com.google.gson.reflect.TypeToken; import java.lang.reflect.Type;
0

Try this,

String a = "[[\"22-1-2017;10:00-19:00\"],[\"22-1-2017;10:00-19:00\"]]";
String[] b = a.replace("[", "").replace("]", "").replace("\"", "").split(",");
List<String> c = Arrays.asList(b);

Output = [22-1-2017;10:00-19:00, 22-1-2017;10:00-19:00]

2 Comments

assuming that the dates does not contains [, ] nor ", it works only if the result must be a flatten List<String>, not a List<List<String>>
String.replace() will return the original value if occurrence not found. Is there any reason to flatten the result to List<List<String>> if it could be flatten to List<String>?
0

I got one solution for your problem, if you want the date in an String Array:

    String s = "[[\"22-1-2017;10:00-19:00\"],[\"22-1-2017;10:00-19:00\"]]";

    // first remove outter brackets
    s = s.substring(1, s.length());
    s = s.substring(0, s.length()-1);

    // Create a Pattern for getting everything within brackets
    Pattern r = Pattern.compile("\\[(.*?)\\]");
    // Now create matcher object.
    Matcher m = r.matcher(s);

    List<String> l = new ArrayList<>();
    while (m.find()) {
        l.add(m.group(1));
    }
    System.out.println(l);

If you want it without the quotations use

    Pattern r = Pattern.compile("\\[\"(.*?)\"\\]");

Hope this helps

Edit: If you use the

    Pattern r = Pattern.compile("\\[\"(.*?)\"\\]");

you do not need to remove the outer brackets at all, as it already looks for content within:

  • opening square brackets with double quotations
  • double quotations with closing square brackets

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.