问题描述
我从 REST JSON 响应中得到以下字符串:
[{uid":10512213,姓名":鲍勃"},{"uid":7208201,姓名":约翰"},{uid":10570,姓名":吉姆"},{uid":1799657,姓名":莎莉"}]其余响应定义来自 Facebook:FB REST Link
我使用的是在 Jetty 中运行的 Google App Engine + GAELYK.
在服务器上的 Groovy 中将上述内容转换为 maps 的 array 的最佳方法是什么.(这可能必须通过响应递归)
我正在寻找不包含大量库的简单内容.(我没有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":1799657, "name":"Sally"}]'//解析响应def list = new JsonSlurper().parseText(restResponse)//打印出来以确保list.each { 打印它 }
下面的旧答案:
使用 JsonSlurper...
读取该响应的示例脚本是:
@Grab('net.sf.json-lib:json-lib:2.3:jdk15')导入 net.sf.json.groovy.JsonSlurper//示例响应数据def restResponse = '[{"uid":10512213, "name":"Bob"},{"uid":7208201, "name":"John"},{"uid":10570, "name":"Jim"},{"uid":1799657, "name":"Sally"}]'//解析响应def list = new JsonSlurper().parseText(restResponse)//打印出来以确保list.each { 打印它 }
输出:
[uid:10512213, name:Bob][用户名:7208201,姓名:约翰][用户名:10570,姓名:吉姆][uid:1799657, 姓名:Sally]
如你所见,list
是一个地图列表,所以如果你只是想要一个名称列表,你可以这样做:
def names = list.name
要在您的 Gaelyk 应用程序中使用它,您只需要下载 json-lib-2.3-jdk15.jar 从这里 并做类似的事情(然后没有@Grab,因为您将在 WEB-INF/lib
文件夹中拥有该 jar.
--编辑--
环顾四周,发现这个页面显示了json-lib的依赖关系
- jakarta commons-lang 2.4
- jakarta commons-beanutils 1.7.0
- jakarta commons-collections 3.2
- ssh linuxs
- ezmorph 1.0.6
测试脚本中的@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 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!