在Groovy中解析JSON数组的数组 [英] Parsing array of JSON arrays in Groovy

查看:2351
本文介绍了在Groovy中解析JSON数组的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  [
{
uid :10512213,
name:Bob
},
{
uid:7208201,
name:John
},
{
uid:10570,
name:Jim
},
{
uid:1799657,
name:Sally
}
]

其余答复的定义来自于Facebook: FB REST链接



我正在使用在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应用程序,您只需要从这里下载json-lib-2.3-jdk15.jar 并且做类似的事情(没有@Grab,那么你将有你的 WEB-INF / lib 文件夹中的jar。



- 编辑 -



环顾四周,发现此页面显示json-lib的依赖关系



测试脚本中的@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

The @Grab in the test script does a lot of background work for you

这篇关于在Groovy中解析JSON数组的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆