编辑:自1.8.0以来,Groovy有一个集成的JsonSlurper:
import groovy.json.JsonSlurper
// Example Response Data
def restResponse = '[{"uid":10512213, "name":"Bob"},{"uid":7208201, "name":"John"},{"uid":10570, "name":"Jim"},{"uid":1799657, "name":"Sally"}]'
// Parse the response
def list = new JsonSlurper().parseText( restResponse )
// Print them out to make sure
list.each { println it }
下面的旧答案:
使用 JsonSlurper...
读取该响应的示例脚本是:
@Grab('net.sf.json-lib:json-lib:2.3:jdk15')
import net.sf.json.groovy.JsonSlurper
// Example Response Data
def restResponse = '[{"uid":10512213, "name":"Bob"},{"uid":7208201, "name":"John"},{"uid":10570, "name":"Jim"},{"uid":1799657, "name":"Sally"}]'
// Parse the response
def list = new JsonSlurper().parseText( restResponse )
// Print them out to make sure
list.each { println it }
此输出:
[uid:10512213, name:Bob]
[uid:7208201, name:John]
[uid:10570, name:Jim]
[uid:1799657, name:Sally]
如您所见,是地图列表,因此,如果您只想要名称列表,则可以执行以下操作:list
def names = list.name
要在Gaelyk应用程序中使用它,您只需要从这里下载json-lib-2.3-jdk15.jar并执行类似操作(无需@Grab,因为您将在文件夹中找到jar。WEB-INF/lib
--编辑--
环顾四周,发现此页面显示了json-lib的依赖项
测试脚本中的@Grab为您完成大量后台工作