將實體對象轉(zhuǎn)換成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"}]
更優(yōu)雅地顯示Json字符串酿雪,可以調(diào)用JsonOutput.prettyPrint方法:
println JsonOutput.prettyPrint(json)
打印輸出為:
[
{
"age": 25,
"name": "Jhon"
},
{
"age": 26,
"name": "Alice"
}
]
將Json字符串轉(zhuǎn)換成實體對象
JsonSlurper jsonSlurper = new JsonSlurper()
def parseList = jsonSlurper.parseText(json)
網(wǎng)絡(luò)訪問請求
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
}
喜歡本篇博客的簡友們,就請來一波點贊惑折,您的每一次關(guān)注,將成為我前進的動力额嘿,謝謝绊诲!