Groovy中的json和xml操作

      網(wǎng)友投稿 806 2025-04-04

      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)

      輸出

      Groovy中的json和xml操作

      [{"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操作

      final?String?xml?=?''' ???? ???????? ???????????? ???????????????? ????????????????????瘋狂Android講義 ????????????????????李剛 ???????????????? ???????????????? ???????????????????第一行代碼 ???????????????????郭林 ??????????????? ??????????????? ???????????????????Android開發(fā)藝術探索 ???????????????????任玉剛 ??????????????? ???????????????? ???????????????????Android源碼設計模式 ???????????????????何紅輝 ??????????????? ??????????? ??????????? ??????????????? ???????????????????Vue從入門到精通 ???????????????????李剛 ??????????????? ??????????? ??????? ???? ''' def?xmlSluper?=?new?XmlSlurper() def?resp?=?xmlSluper.parseText(xml) println?resp.value.books[1].book[0].title.text()

      輸出

      Vue從入門到精通

      //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

      輸出

      第一行代碼郭林

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

      ?Java ?Groovy ?JavaScript ?

      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都是用實體對象動態(tài)生成的定義class如下

      //對應xml中的Langs結(jié)點 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結(jié)點 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

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

      華為云APP

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔相應法律責任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔相應法律責任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。

      上一篇:如何上傳文件(如何上傳文件到共享盤里)
      下一篇:管理系統(tǒng)的軟件是什么意思?
      相關文章
      亚洲一区二区高清| 亚洲日韩国产一区二区三区| 久久精品亚洲福利| 校园亚洲春色另类小说合集| 一区二区亚洲精品精华液| 亚洲日本在线免费观看| 久久久无码精品亚洲日韩京东传媒 | 亚洲国产高清在线一区二区三区| 亚洲欧美熟妇综合久久久久| 亚洲中文字幕人成乱码| 亚洲一区二区三区免费视频| 亚洲国产日产无码精品| 亚洲av成人一区二区三区| 亚洲成人福利在线观看| 亚洲午夜电影一区二区三区| 亚洲免费观看在线视频| 色婷五月综激情亚洲综合| 亚洲第一页中文字幕| 亚洲一区二区三区免费观看| 激情亚洲一区国产精品| 亚洲中文字幕久久久一区| 亚洲国产成人久久精品大牛影视 | 亚洲国产精品无码专区影院| 亚洲va无码专区国产乱码| 亚洲av不卡一区二区三区| 国产亚洲人成无码网在线观看| 久久久久久亚洲精品| 久久精品国产亚洲精品2020| 亚洲黄色在线播放| 亚洲伊人久久大香线焦| 亚洲人成网国产最新在线| 亚洲AV无码成人网站在线观看| 国产成人综合久久精品亚洲| 亚洲性日韩精品国产一区二区| 亚洲中文字幕在线观看| 久久久无码精品亚洲日韩蜜桃| 亚洲视频在线观看视频| 亚洲视频一区二区三区四区| 亚洲精华液一二三产区| 亚洲精品国产精品乱码不卞 | 国产精品亚洲专区在线观看 |