免费教程_免费网赚教程_破解版软件-寂涯网络学习基地

当前位置: 主页 > 系统综合 > 各类编程 > groovy操作json

groovy操作json

时间:2011-10-19 16:29来源: 整理:寂涯网络 点击:

在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,不是玻璃”

本页地址 http://www.jybase.net/biancheng/20111018182.html

百度搜索更多

谷歌搜索更多

顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------

评价:
昵称: 验证码:点击我更换图片
推荐内容
赞助商
赞助商


关于本站免责声明视频更新google百度地图视频地图RRS订阅

如有什么问题请在本站留言,或发邮件到 hxt167#foxmail.com