我是靠谱客的博主 标致棒球,这篇文章主要介绍Groovy语法学习(六)JSON、XML使用,现在分享给大家,希望可以做个参考。

groovy对于json和xml的生成和解析不需要额外导入库了,直接调用api使用。

一、JSON

(一) JSON字符串创建
1.使用JsonBuilder类

JsonBuilder有call()方法传入closure,所以可以接像下面这样写,相当于调用call方法。

复制代码
1
2
3
4
5
6
7
8
9
def builder = new JsonBuilder() builder{ name'jabamiYu' age 12 } assert builder.toString()=="{"name":"jabamiYu","age":12}"
2.使用对象直接转字符串
复制代码
1
2
3
4
5
6
7
8
9
class Franxx{ def name def sex } def p =new Franxx(name:"zero two",sex:"female") println new JsonBuilder(p) println JsonOutput.toJson(p) println JsonOutput.prettyPrint(JsonOutput.toJson(p))

结果:

复制代码
1
2
3
4
5
6
{"sex":"female","name":"zero two"} {"sex":"female","name":"zero two"} { "sex": "female", "name": "zero two" }

更多稍微复杂的用法,大家可以去参考对应几个类注释,注释上都有例子。

二、XML

(一) 解析xml

使用安卓的Manifest文件

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.ty"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <meta-data android:name="hello" android:value="1"/> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>

解析

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def parser = new XmlParser().parse(new File("AndroidManifest.xml")) //创建命名空间 def ns = new Namespace('http://schemas.android.com/apk/res/android','android') Node nodeactivity = parser.application[0].activity[0] println nodeactivity.attributes()[ns.name] //获得application节点 Node node = parser.application[0] //NodeList Node meta = node.'meta-data'[0] node.remove(meta) node.appendNode('meta-data',[(ns.name):'a',(ns.value):'b',(ns.hh):'hh']) new XmlNodePrinter(new PrintWriter(new File("replace.xml"))).print(parser)

replace.xml

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
<manifest package="com.example.ty"> <application android:allowBackup="true" xmlns:android="http://schemas.android.com/apk/res/android" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" 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> <meta-data android:name="a" android:value="b" android:hh="hh"/> </application> </manifest>
(二) 生成xml
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def fw = new FileWriter(new File("normal.xml")) def builder = new MarkupBuilder(fw) builder.html{ mkp.comment("test") head("hello",m:'a'){ title("com.jabamiYu") } body{ } } def sb = new StreamingMarkupBuilder() sb.encoding = 'UTF-8' def closure = { mkp.xmlDeclaration() html{ head(id:1){ } } } def sw = sb.bind(closure) println sw.toString()

结果:
normal.xml文件

复制代码
1
2
3
4
5
6
<html><!-- test --> <head m='a'>hello <title>com.jabamiYu</title> </head> <body /> </html>

打印:

复制代码
1
2
<?xml version='1.0' encoding='UTF-8'?> <html><head id='1'></head></html>

只总结了一些简单用法,更加复杂的用法同样可以通过查看注释。

最后

以上就是标致棒球最近收集整理的关于Groovy语法学习(六)JSON、XML使用的全部内容,更多相关Groovy语法学习(六)JSON、XML使用内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(75)

评论列表共有 0 条评论

立即
投稿
返回
顶部