Groovy map dot keys to nested map

前端 未结 1 1837
青春惊慌失措
青春惊慌失措 2021-01-14 09:04

I have a map with keys in dot notation, but I need it as a nested map.

[test.key.one: \'value1\', text.key.two: \'value2\']

Now the result

相关标签:
1条回答
  • 2021-01-14 09:45

    There is one handy class: groovy.util.ConfigSlurper

    def map = ['test.key.one': 'value1', 'test.key.two': 'value2']
    def props = new Properties()
    props.putAll(map)
    println new ConfigSlurper().parse(props) // [test:[key:[two:value2, one:value1]]]
    

    The only drawback is that it expects java.util.Properties instance, so you need to create one from the map.

    0 讨论(0)
提交回复
热议问题