将嵌套的json转换为groovy中的点线贴图

时间:2017-11-17 18:31:40

标签: groovy

如何在groovy中将嵌套的json转换为虚线贴图?

canv = Canvas(root, width=80, height=80, bg='white')
canv.grid(row=2, column=3)

img = PhotoImage(file="bll.jpg")
canv.create_image(20,20, anchor=NW, image=img)

输出:

import groovy.json.*

def slurper = new groovy.json.JsonSlurper()
def json = slurper.parseText('{"person"{"name":"Guillaume","age":33,"pets":["dog","cat"]}}')

println(json)

但我正在寻找以下输出

[person:[name:Guillaume, age:33, pets:[dog, cat]]]

感谢任何帮助?

1 个答案:

答案 0 :(得分:0)

您可以通过collectMany进行尝试:

def result = json.collectMany{ 
    k, v -> v.collect {  
        l -> if(l.key != null && l.value != null  ) {
            json.find().key+"."+l.key+":"+l.value
        }
    }
}
println(result)​

这是输出:

[person.name:Guillaume, person.age:33, person.pets:[dog, cat]]

祝你好运!