Groovy: isn't there a stringToMap out of the box?

前端 未结 5 1261
独厮守ぢ
独厮守ぢ 2020-12-30 23:30

as a tcl developer starting with groovy, I am a little bit surprised about the list and map support in groovy. Maybe I am missing something here.

I am used to conver

5条回答
  •  猫巷女王i
    2020-12-31 00:13

    If you don't want to use evaluate(), do instead:

    def stringMap = "['a':2,'b':4]"
    stringMap = stringMap.replaceAll('\\[|\\]','')
    def newMap = [:]
    stringMap.tokenize(',').each {
    kvTuple = it.tokenize(':')
    newMap[kvTuple[0]] = kvTuple[1]
    }
    println newMap
    

提交回复
热议问题