-1

I get from an url a result as:

[[1509321600000,35166.44],[1509408000000,35224.31],[1509580800000,35234.60]]

it is a String, and I want to transform to an array of arrays.

Does a function exist for that? Or do I have to use explode function and create the array manually?

Per Huss
  • 4,755
  • 12
  • 29
user1450740
  • 731
  • 10
  • 26

2 Answers2

2

As suggested in the comments, as the string is a valid JSON array declaration, parsing it as JSON would probably be easiest and most maintainable in the future. Using the google/gson library the code would look like this:

String string = "[[1509321600000,35166.44],[1509408000000,35224.31],[1509580800000,35234.60]]";
String[][] array = new Gson().fromJson(string, String[][].class);
Per Huss
  • 4,755
  • 12
  • 29
0

There is no function provided by default, but you can do the following by using Streams (if your String has exactly that format you specified):

String[][] a = 
    Arrays.stream(s.substring(2, s.length()-2).split("\\],\\["))
        .map(sub -> sub.split(","))
        .toArray(String[][]::new);

Here, you cut off the first and last two brackets (.substring(2, s.length()-2)), then split your String (.split("\\],\\["))(which results in an array of Strings of format "asdf,fdsa".

Array.stream creates a stream of that, map splits those Strings to arrays, and then you can collect them with the toArray-method.

Printing this array of arrays leads to:

1509321600000   35166.44    
1509408000000   35224.31    
1509580800000   35234.60    
Thomas Böhm
  • 1,456
  • 1
  • 15
  • 27