Parsing a nested-array to such a String is rather simple:
Arrays.deepToString(array).replace(" ", "");
Converting this String to an infinitely nested-list is a bit more tricky. The easiest solution would probably be to use recursion:
/**
* The following code is only for demonstration purposes.
* It does neither do any validation on the input String
* nor work with more than one digit numbers.
*/
static int index = 0; // the position in the String
Object buildList(String nestedList) {
List<Object> list = new ArrayList<>();
while (index < nestedList.length()) {
char c = nestedList.charAt(index++);
if (c == '[') // add a sub-list via a recursive call
list.add(buildList(nestedList));
else if (c == ']') // stop building the list
break;
else if (c == ',') {} // do nothing
else // add an element to the list
list.add(c);
}
return list;
}
Example call:
System.out.println(buildList("[3,[4,3],5]")); // prints [3, [4, 3], 5]
Note:
Even though the above code does what (at least I think) you want to achieve, it may not be advisable to use such a data-structure in practice, since the access to the nested-lists is quite complicated and involves some casting.
A better solution would probably be to use some kind of tree-datastructure where each node has a list of values including links to other nodes.
(see: http://en.wikipedia.org/wiki/Tree_(data_structure))