package jsoncaozuo
import chijiuzhuru.ApplicationManager
import chijiuzhuru.PersonManager
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import groovy.xml.MarkupBuilder
ApplicationManager.init();
def list=[PersonManager.createPerson('li',34),PersonManager.createPerson('ch',43)]
println(JsonOutput.toJson(list))//[{"age":34,"name":"li"},{"age":43,"name":"ch"}]
//对象转json
def json=JsonOutput.toJson(list)
JsonOutput.prettyPrint(json)//优化输出
//JSON转对象
def jsonSlpuer=new JsonSlurper()
def jsonStr=jsonSlpuer.parseText(json)
println(jsonStr)//[[age:34, name:li], [age:43, name:ch]]
//grovvy没有自己的网络请求框架,但是可以使用java 的,包括引用第三方类库都一模一样
def result=getNetWorkData('https://appdev7.hexindai.com/home')
println(result)
def getNetWorkData(String url){
def connection =new URL(url).openConnection()
connection.setRequestMethod('GET')
connection.connect()
def response=connection.content.text
def jsonSluper=new JsonSlurper()
jsonSluper.parseText(response)
}
//grovvy操作xml文件
final String xml='''
<response version-api="2.0">
<value>
<books id="1" classification="android">
<book available="20" id="1">
<title>羚羊峡谷</title>
<author id="1">黎明</author>
</book>
<book available="31" id="2">
<title>托斯卡纳</title>
<author id="2">地方</author>
</book>
<book available="24" id="3">
<title>意大利</title>
<author id="3">列表</author>
</book>
</books>
<books id="2" classification="ios">
<book available="10" id="1">
<title>美国</title>
<author id="1">黎明</author>
</book>
<book available="50" id="2">
<title>日本</title>
<author id="2">狗</author>
</book>
<book available="40" id="3">
<title>中国</title>
<author id="3">大佬</author>
</book>
</books>
</value>
</response>
'''
//解析xml
def xmlSluper=new XmlSlurper()
def response=xmlSluper.parseText(xml)
println(response.value.books[0].book[0].title)//羚羊峡谷
println(response.value.books[1].book[1].@available)//50
//遍历查询
def bookList=[]
response.value.books.each{
books->books.book.each{
book->def atuthor=book.author.text()
if(atuthor.equals('黎明')){
bookList.add(book.title.text())
}
}
}
println(bookList.toListString())//[羚羊峡谷, 美国]
//更便利的深度递归遍历
//深度遍历的另一种写法response.'**'.findAll
def bookList2=response.depthFirst().findAll{
book->
return book.author.text()=='黎明'
}
println(bookList2.toListString())//[羚羊峡谷黎明, 美国黎明]
//广度遍历
//广度遍历,children的另一种写法 response.value.books.'*'.findAll
def name=response.value.books.children.findAll{
node->node.name=='book' && node.@available=='40'
}.collect{
node->node.title.text()
}
println(name.toListString())//中国
//生成xmL格式的数据(静态数据写法)
def sw=new StringWriter()
def xmlBuilder=new MarkupBuilder(sw)
//创建根节点langs(.langs是个伪方法,根节点的名字就是方法名字
xmlBuilder.langs(type:'currnet',count:'3',mainstream:'true'){
language(flavor:'static',version:'1.3',value:'java'){
age(value:16)
}
language(flavor:'dynamic',version:'1.3',value:'groovy'){
age(value:10)
}
language(flavor:'dynamic',version:'1.3',value:'js')
}
println(sw)//结果:
/*<langs type='currnet' count='3' mainstream='true'>
<language flavor='static' version='1.3' value='java'>
<age value='16' />
</language>
<language flavor='dynamic' version='1.3' value='groovy'>
<age value='10' />
</language>
<language flavor='dynamic' version='1.3' value='js' />
</langs>*/
//实体转换(动态数据)
def langs=new langs()
xmlBuilder.langs(type:langs.type,count:langs.count,mainstream:langs.mainstream){
langs.language.each{
lang->Language(flavor:lang.flavor,version:lang.version,value:lang.value)
}
}
class langs{
String type='current'
int count=3
boolean mainstream=true
def language=[
new Language(flavor:'static',version:'1.8',value:'java'),
new Language(flavor:'dynamic',version:'1.6',value:'groovy'),
new Language(flavor:'dynamic',version:'1.7',value:'js'),
]
}
class Language{
String flavor
String version
String value
}
println(sw)
/*<langs type='current' count='3' mainstream='true'>
<Language flavor='static' version='1.8' value='java' />
<Language flavor='dynamic' version='1.6' value='groovy' />
<Language flavor='dynamic' version='1.7' value='js' />
</langs>*/
原文链接:http://www.apkbus.com/blog-953329-77639.html
打开App,阅读手记