Gradle系列六之Json操作

作者: zhang_pan | 来源:发表于2019-05-27 17:13 被阅读0次
将实体对象转换成Json字符串
def list = [new Person(name: 'Jhon', age: 25),
            new Person(name: 'Alice', age: 26)]
def json = JsonOutput.toJson(list)
println json

打印输出为:

[{"age":25,"name":"Jhon"},{"age":26,"name":"Alice"}]

更优雅地显示Json字符串,可以调用JsonOutput.prettyPrint方法:

println JsonOutput.prettyPrint(json)

打印输出为:

[
    {
        "age": 25,
        "name": "Jhon"
    },
    {
        "age": 26,
        "name": "Alice"
    }
]
将Json字符串转换成实体对象
JsonSlurper jsonSlurper = new JsonSlurper()
def parseList = jsonSlurper.parseText(json)
网络访问请求
getNetworkData('https://home.firefoxchina.cn/?from=extra_start')
static def getNetworkData(String url) {
    def connection = new URL(url).openConnection() as HttpURLConnection
    connection.setRequestMethod('GET')
    connection.connect()
    def response = connection.content.text
    println response
}

喜欢本篇博客的简友们,就请来一波点赞,您的每一次关注,将成为我前进的动力,谢谢!

相关文章

网友评论

    本文标题:Gradle系列六之Json操作

    本文链接:https://www.haomeiwen.com/subject/ycwhtctx.html