我想使用闭包方式来制作以下 json:
{
"root": [
{
"key": "testkey",
"value": "testvalue"
}
]
}
我正在使用以下语法:
new JsonBuilder().root {
'key'(testKey)
'value'(testValue)
}
但它产生:
{
"root": {
"key": "testkey",
"value": "testvalue"
}
}
最佳答案
你可以写如下:
def json = new groovy.json.JsonBuilder()
json {
root (
[
{
key ('color')
value ('orange')
}
]
)
}
println json.toPrettyString()
注意数组如何传递给
root
在上面。
关于arrays - 如何在 groovy 中使用 JsonBuilder 创建数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49366249/