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

前端 未结 5 1247
独厮守ぢ
独厮守ぢ 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条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-31 00:20

    You might want to try a few of your scenarios using evaluate, it might do what you are looking for.

    def stringMap = "['a':2,'b':4]"
    def map = evaluate(stringMap)
    
    assert map.a == 2
    assert map.b == 4
    
    def stringMapNested = "['foo':'bar', baz:['alpha':'beta']]"
    def map2 = evaluate(stringMapNested)
    
    assert map2.foo == "bar"
    assert map2.baz.alpha == "beta"
    

提交回复
热议问题