groovy操作json
在groovy1.8中内置了对json格式数据的至此;
使对json的操作变得非常简捷方便了
Java代码
def builder = new JsonBuilder()
//如同构建对象般
builder.pepole{
person {
firstName 'leng'
lastName 'feng'
//传入map
address(
city: 'Shanghai',
country: 'China',
zip: 12345,
)
married true
//传如list
conferences 'JavaOne', 'Gr8conf'
}
}
//以树形结构输出
println JsonOutput.prettyPrint(builder.toString())
String json = """
{
"pepole": {
"person": {
"firstName": "leng",
"lastName": "feng",
"address": {
"city": "Shanghai",
"country": "China",
"zip": 12345
},
"married": true,
"conferences": [
"JavaOne",
"Gr8conf"
]
}
}
}
"""
//类似XmlSlurper
def root = new JsonSlurper().parseText(json)
assert root instanceof Map
assert root.person.conferences instanceof List
assert root.person.firtsName == 'leng'
assert root.person.conferences[1] == 'Gr8conf'
作者“I am Berdy,不是玻璃”
补充:软件开发 , Java ,