Groovy数据解析
一、json解析
class People {
String name
Integer age
}
def personList = [new People(name: '哈哈', age: 28), new People(name: 'groovy', age: 6)]
//将对象解析成json字符串
def result = JsonOutput.toJson(personList)
println result //[{"age":28,"name":"哈哈"},{"age":6,"name":"groovy"}]
println JsonOutput.prettyPrint(result) //格式化输出
/**
[
{
"age": 28,
"name": "java"
},
{
"age": 6,
"namgovy"
}
]
*/
//将json字符串传化为对象
def jsonSlurper = new JsonSlurper()
def objectResult = (ArrayList<Person>) jsonSlurper.parseText(result)
Person p = objectResult[0]
println "the name is $p.name,the age is $p.age" //输出结果:the name is java,the age is 28
def getNetWorkData(String url) {
//发送http请求(此处用原生的,也可导入okHttp库进行网络操作)
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection()
connection.setConnectTimeout(10000)
connection.setRequestMethod('GET')
connection.connect()
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()))
StringBuffer stringBuffer = new StringBuffer()
String json = ""
while ((json = bufferedReader.readLine()) != null) {
stringBuffer.append(json)
}
String rsp = new String(stringBuffer.toString().getBytes(), "UTF-8")
//解析
def jsonSlurper = new JsonSlurper()
return jsonSlurper.parseText(rsp)
}
/*
{"data":[{"desc":"一起来做个App吧","id":10,"imagePath":"http://www.wanandroid.com/blogimgs/50c115c2-cf6c-4802-aa7b-a4334de444cd.png","isVisible":1,"order":2,"title":"一起来做个App吧","type":0,"url":"http://www.wanandroid.com/blog/show/2"},{"desc":"","id":4,"imagePath":"http://www.wanandroid.com/blogimgs/ab17e8f9-6b79-450b-8079-0f2287eb6f0f.png","isVisible":1,"order":0,"title":"看看别人的面经,搞定面试~","type":1,"url":"http://www.wanandroid.com/article/list/0?cid=73"},{"desc":"","id":3,"imagePath":"http://www.wanandroid.com/blogimgs/fb0ea461-e00a-482b-814f-4faca5761427.png","isVisible":1,"order":1,"title":"兄弟,要不要挑个项目学习下?","type":1,"url":"http://www.wanandroid.com/project"},{"desc":"","id":6,"imagePath":"http://www.wanandroid.com/blogimgs/62c1bd68-b5f3-4a3c-a649-7ca8c7dfabe6.png","isVisible":1,"order":1,"title":"我们新增了一个常用导航Tab~","type":1,"url":"http://www.wanandroid.com/navi"},{"desc":"","id":2,"imagePath":"http://www.wanandroid.com/blogimgs/90cf8c40-9489-4f9d-8936-02c9ebae31f0.png","isVisible":1,"order":2,"title":"JSON工具","type":1,"url":"http://www.wanandroid.com/tools/bejson"},{"desc":"","id":5,"imagePath":"http://www.wanandroid.com/blogimgs/acc23063-1884-4925-bdf8-0b0364a7243e.png","isVisible":1,"order":3,"title":"微信文章合集","type":1,"url":"http://www.wanandroid.com/blog/show/6"}],"errorCode":0,"errorMsg":""}
*/
class Root {
List<Data> data
int errorCode
String errorMsg
class Data {
String desc
int id
String imagePath
int isVisible
int order
String title
int type
String url
}
}
Root root = getNetWorkData("http://www.wanandroid.com/banner/json")
println root.data[0].imagePath //输出结果:http://www.wanandroid.com/blogimgs/50c115c2-cf6c-4802-aa7b-a4334de444cd.png
//以上是类似java的做法,而在Groovy中,不需要写实体类,可直接使用里面的字段
def response = getNetWorkData("http://www.wanandroid.com/banner/json")
println response.data[0].imagePath //输出结果:http://www.wanandroid.com/blogimgs/50c115c2-cf6c-4802-aa7b-a4334de444cd.png
网友评论