问题描述
[
{
uid :10512213,
name:Bob
},
{
uid:7208201,
name:John
},
{
uid:10570,
name:Jim
},
{
uid:1799657,
name:Sally
}
]
其余答复的定义来自于Facebook:
我正在使用在Jetty运行的Google App Engine + GAELYK。
转换以上内容的最佳方式是什么?在服务器上的Groovy中进入地图 数组。 (这可能必须通过响应来递归)
我正在寻找一些不包含很多图书馆的东西。 (我没有maven)
编辑:Groovy自1.8.0以来有一个集成的JsonSlurper:
import groovy.json.JsonSlurper
//示例响应数据
def restResponse ='[{uid: 10512213,name:Bob},{uid:7208201,name:John},{uid:10570,name:Jim},{uid name:Sally}]'
//解析响应
def list = new JsonSlurper()。parseText(restResponse)
//打印他们出来确保
list.each {println it}
以下回答: / h2>
使用JsonSlurper ...
读取该响应的示例脚本将是:
@Grab('net.sf.json-lib:json-lib:2.3:jdk15')
import net.sf.json。 groovy.JsonSlurper
//示例响应数据
def restResponse ='[{uid:10512213,name:Bob},{uid:7208201,name :John},{uid:10570,name:Jim},{uid:1799657,name:S ally}]'
//解析响应
def list = new JsonSlurper()。parseText(restResponse)
//打印出来确保
list.each {println it}
输出:
[uid:10512213,name:Bob]
[uid:7208201,name:John]
[uid:10570,name: Jim]
[uid:1799657,name:Sally]
如你所见, code> list 是地图的列表,所以如果你只想要一个名单列表,你可以这样做:
def names = list.name
您的Gaelyk应用程序,您只需要并且做类似的事情(没有@Grab,那么你将有你的 WEB-INF / lib
文件夹中的jar。
- 编辑 -
环顾四周,发现
测试脚本中的@Grab为您提供了许多后台工作
I have the following string from a REST JSON response:
[
{
"uid":10512213,
"name":"Bob"
},
{
"uid":7208201,
"name":"John"
},
{
"uid":10570,
"name":"Jim"
},
{
"uid":1799657,
"name":"Sally"
}
]
The rest response definition is from Facebook: FB REST Link
I am using Google App Engine + GAELYK which runs in Jetty.
What is the best way to convert the above into array of maps in Groovy on the Server. (This would probably have to recurse through the response)
I am looking for something easy that doesn't include a lot of libraries. (I dont have maven)
EDIT: Groovy since 1.8.0 has an integrated 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 }
Old answer below:
Use JsonSlurper...
An example script to read that response would be:
@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 }
This outputs:
[uid:10512213, name:Bob]
[uid:7208201, name:John]
[uid:10570, name:Jim]
[uid:1799657, name:Sally]
As you can see, list
is a list of Maps, so if you just wanted a list of the names for example, you could just do:
def names = list.name
To use this in your Gaelyk app, you should just need to download json-lib-2.3-jdk15.jar from here and do something similar (without the @Grab then, as you'll have the jar in your WEB-INF/lib
folder.
--edit--
Looking around, found this page showing the dependencies for json-lib
- jakarta commons-lang 2.4
- jakarta commons-beanutils 1.7.0
- jakarta commons-collections 3.2
- ssh linuxs
- ezmorph 1.0.6
The @Grab in the test script does a lot of background work for you
这篇关于在Groovy中解析JSON数组的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!