Prabin Upreti Prabin Upreti - 2 months ago 7
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

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('\\]', '}'))