Prabin Upreti Prabin Upreti - 1 year ago 182
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('\\]', '}'))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download