Groovy 序列化 JSON XML 解析

Groovy 序列化

本文链接:https://blog.csdn.net/feather_wch/article/details/131623398

JSON

class Person {
    String name
    Integer age
}

//对象转成json字符串
def list=[new Person(name:'jett',age:18),
            new Person(name:'lance',age:18)]
println JsonOutput.toJson(list)
//对象转成json字符串: 格式化
def json=JsonOutput.toJson(list)
println JsonOutput.prettyPrint(json)

//json字符串转成对象
def jsonSluper=new JsonSlurper()
def object=jsonSluper.parse("[{\"age\":18,\"name\":\"jett\"},{\"age\":18,\"name\":\"lance\"}]".getBytes())
println object

def object2=jsonSluper.parse("[{\"abc\":\"jett\"}]".getBytes())
println object2.abc

XML

final String xml='''
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.jvm_demo_20200601">
    <test>12345</test>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity2">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
'''

//解析XML数据
def xmlSluper=new XmlSlurper()
def result=xmlSluper.parseText(xml)
println result.@package
println result.test.text()
//读取有域名空间的节点
result.declareNamespace('android':'http://schemas.android.com/apk/res/android')
println result.application.@'android:allowBackup'
println result.application.activity[0].@'android:name'
println result.application.activity[1].@'android:name'

//遍历XML节点
result.application.activity.each{activity ->
    println activity.@'android:name'
}

/**
 * 生成XML格式数据
 * <html>
 *     <title id='123',name='android'>xml生成
 *          <person></person>
 *     </title>
 *     <body name='java'>
 *         <activity id='001' class='MainActivity'>abc</activity>
 *         <activity id='002' class='SecActivity'>abc</activity>
 *     </body>
 * </html>
 */
def sw=new StringWriter()
def xmlBuilder=new MarkupBuilder(sw)
xmlBuilder.html(){
    title(id:'123',name:'android','xml生成'){
        person()
    }
    body(name:'java'){
        activity(id:'001',class:'MainActivity','abc')
        activity(id:'002',class:'SecActivity','abc')
    }
}
println sw
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猎羽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值