Groovy中的json和xml操作
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操作
final?String?xml?=?''' ????
輸出
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文檔
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)容。