Prabin Upreti Prabin Upreti - 1 year ago 83
Groovy Question

Convert Map Object from String "[a:12,b:[a:b,c:d]]"

Is there any out of box method in java (groovy) that converts the string like

String s = "[a:12,b:[a:b,c:d]]";

to a Map object with key value pairs.

Answer Source

You can convert this string to JSON-like format, and simple parse it:

String s = "[a:12,b:[a:b,c:d]]";
def result = new JsonSlurper().setType(JsonParserType.LAX).parseText(s.replaceAll('\\[', '{').replaceAll('\\]', '}'))