Groovy中的json和xml操作

举报
月色很美 发表于 2018/12/19 08:45:37 2018/12/19
【摘要】 json操作json ->objectfinal String myJson ='''{"code":200,"body":{"search_result":[{"id":"1393","name":"东京食尸鬼漫画","cover_url":"http://img.1whour.com/xpic/东京食尸鬼.jpg","other_1":"","other_2":""},{"id":"24...

json操作

json ->object

final String myJson ='''
{"code":200,"body":{"search_result":[{"id":"1393","name":"东京食尸鬼漫画","cover_url":"http://img.1whour.com/xpic/东京食尸鬼.jpg","other_1":"","other_2":""},{"id":"2402","name":"东京都立咒术学校漫画","cover_url":"http://img.1whour.com/xpic/东京都立咒术学校.jpg","other_1":"","other_2":""},{"id":"1107","name":"大东京玩具箱漫画","cover_url":"http://img.1whour.com/xpic/大东京玩具箱.jpg","other_1":"","other_2":""},{"id":"2026","name":"东京小红帽漫画","cover_url":"http://img.1whour.com/xpic/东京小红帽.jpg","other_1":"","other_2":""},{"id":"879","name":"东京ESP漫画","cover_url":"http://img.1whour.com/xpic/djESP.jpg","other_1":"","other_2":""},{"id":"2064","name":"东京暗鸦SwordOfSong漫画","cover_url":"http://img.1whour.com/xpic/东京暗鸦Sword.jpg","other_1":"","other_2":""},{"id":"943","name":"东京乌鸦漫画","cover_url":"http://img.1whour.com/xpic/东京乌鸦.jpg","other_1":"","other_2":""},{"id":"1954","name":"东京奇迹少年漫画","cover_url":"http://img.1whour.com/xpic/东京奇迹少年.jpg","other_1":"","other_2":""},{"id":"1106","name":"东京玩具箱漫画","cover_url":"http://img.1whour.com/xpic/东京玩具箱.jpg","other_1":"","other_2":""},{"id":"430","name":"东京MewMew漫画","cover_url":"http://img.1whour.com/xpic/0djmm.jpg","other_1":"","other_2":""},{"id":"358","name":"东京80年代漫画","cover_url":"http://img.1whour.com/xpic/363.jpg","other_1":"","other_2":""}]}}
'''
def jsonSluper = new JsonSlurper()
def object = jsonSluper.parseText(myJson)
println object.body.search_result[0]

输出

[id:1393, name:东京食尸鬼漫画, cover_url:
http://img.1whour.com/xpic/
东京食尸鬼.jpg, other_1:, other_2:]
东京食尸鬼漫画

object -> json

def cars = [new Car(name: 'qihu',money: 2000),
           new Car(name: 'qq',money: 1000),
           new Car(name: 'haha',money: 3000)]
def json = JsonOutput.toJson(cars)
println JsonOutput.prettyPrint(json)

输出

[{"money":2000,"name":"qihu"},{"money":1000,"name":"qq"},{"money":3000,"name":"haha"}]
[    {        "money": 2000,        "name": "qihu"    },    {        "money": 1000,        "name": "qq"    },    {        "money": 3000,        "name": "haha"    }]

xml操作

解析xml数据

final String xml = '''
    <response version-api="2.0">
        <value>
            <books id="1" classification="android">
                <book available="20" id="1">
                    <title>疯狂Android讲义</title>
                    <author id="1">李刚</author>
                </book>
                <book available="14" id="2">
                   <title>第一行代码</title>
                   <author id="2">郭林</author>
               </book>
               <book available="13" id="3">
                   <title>Android开发艺术探索</title>
                   <author id="3">任玉刚</author>
               </book>
                <book available="5" id="4">
                   <title>Android源码设计模式</title>
                   <author id="4">何红辉</author>
               </book>
           </books>
           <books id="2" classification="web">
               <book available="10" id="1">
                   <title>Vue从入门到精通</title>
                   <author id="4">李刚</author>
               </book>
           </books>
       </value>
    </response>
'''
def xmlSluper = new XmlSlurper()
def resp = xmlSluper.parseText(xml)
println resp.value.books[1].book[0].title.text()

输出

Vue从入门到精通

xml数据简单查找

//xmlresp同上 
def list = []
resp.value.books.each {books->
    books.book.each { book ->
        def author =  book.author.text()
        if (author == '李刚'){
            list.add(book)
        }
    }
}
println list.toListString()

输出

[疯狂Android讲义李刚, Vue从入门到精通李刚]

深度遍历

//深度遍历
def titles1 = resp.depthFirst().findAll {book ->
    return book.author.text() == '李刚'
}
def titles2 = resp.depthFirst().findAll {node ->
    node.name() == 'book' && node.@id == '2'
}
println titles1
println titles2

输出 

[疯狂Android讲义李刚, Vue从入门到精通李刚]
[第一行代码郭林]

广度遍历

//广度遍历
def name = resp.value.books.children().findAll { node->
    node.name() == 'book' && node.@id=='2'
}
println name

输出 

第一行代码郭林

生成xml

Groovy中生成xml用的是MarkupBuilder。例如要生成如下的xml文档

<langs type='current' count='3' mainstream='true'>
 <language flavor='static' version='1.5'>Java</language>
 <language flavor='dynamic' version='1.6.0'>Groovy</language>
 <language flavor='dynamic' version='1.9'>JavaScript</language>
 </langs>
def sw = new StringWriter()
def xmlBuilder = new MarkupBuilder(sw)
xmlBuilder.langs(type: 'current', count: '3', mainstream: 'true') {
    language(flavor: 'static', version: '1.5', "Java")
    language(flavor: 'dynamic', version: '1.6', "Groovy")
    language(flavor: 'dynamic', version: '1.9', "JavaScript")
}
println sw

一般我们生成xml都是用实体对象动态生成的定义class如下

//对应xml中的Langs结点
class Langs {
    String type = "current"
    int count = 3
    boolean mainstream = true
    def language = [new Language(flavor: "static", version: 1.5, value: "java"),
                    new Language(flavor: "dynamic", version: 1.6, value: "Groovy"),
                    new Language(flavor: "dynamic", version: 1.9, value: "JavaScript")]
}
//对应xml中的language结点
class Language {
    String flavor
    String version
    String value
}
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.value,
                lang.value)
    }
}
println sw

上面的生成方式也是可以的

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。