你的JSON & AJAX 满分学习文章,请收下(你的计算机配置似乎是正确的,但该设备或资源没有响应)
678
2022-05-30
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都是用实体对象动态生成的定义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
上面的生成方式也是可以的
华为云APP
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。