我们可以通过生成Groovy的JSON建设者的对象类型JSON:
def builder = new groovy.json.JsonBuilder()
def root = builder.people {
person {
firstName 'Guillame'
lastName 'Laforge'
// Named arguments are valid values for objects too
address(
city: 'Paris',
country: 'France',
zip: 12345,
)
married true
// a list of values
conferences 'JavaOne', 'Gr8conf'
}
}
def jsonStr = builder.toString()
我喜欢这种类型的语法,而是如何建立一个数组类型JSON?
如
[
{"code": "111", "value":"222"},
{"code": "222", "value":"444"}
]
我发现了一些文件,其中说我们应该用JsonBuilder()
构造函数:
def mydata = [ ["code": "111", "value":"222"],["code": "222", "value":"444"] ]
def builder = new groovy.json.JsonBuilder(mydata)
def jsonStr = builder.toString()
但我更喜欢第一种语法。 它是能够使用它产生数组类型JSON?