Groovy does not need to import additional libraries for the generation and parsing of json and xml, and directly calls the api for use.
1. JSON
(1) JSON string creation
1. Use the JsonBuilder class
JsonBuilder has the call() method to pass into the closure, so it can be written like the following, which is equivalent to calling the call method.
def builder = new JsonBuilder()
builder{
name'jabamiYu'
age 12
}
assert builder.toString()=="{\"name\":\"jabamiYu\",\"age\":12}"
2. Use objects to directly convert strings
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))
result:
{"sex":"female","name":"zero two"}
{"sex":"female","name":"zero two"}
{
"sex": "female",
"name": "zero two"
}
For more complex usage, you can refer to the corresponding class annotations, and there are examples in the annotations.
Second, XML
(1) Parse xml
Using the Android Manifest file
<?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>
Parse
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
<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>
(2) Generate xml
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()
Result:
normal.xml file
<html><!-- test -->
<head m='a'>hello
<title>com.jabamiYu</title>
</head>
<body />
</html>
Print:
<?xml version='1.0' encoding='UTF-8'?>
<html><head id='1'></head></html>
Only some simple usages are summarized, and more complex usages can also be viewed by viewing the comments.