Java 在 Groovy 中解析 JSON 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3855232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 05:54:12  来源:igfitidea点击:

Parsing array of JSON arrays in Groovy

javajsongoogle-app-enginefacebookgroovy

提问by Tihom

I have the following string from a REST JSON response:

我从 REST JSON 响应中得到以下字符串:

[
   {
      "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

其余响应定义来自 Facebook:FB REST Link

I am using Google App Engine + GAELYK which runs in Jetty.

我正在使用在 Jetty 中运行的 Google App Engine + GAELYK。

What is the best way to convert the above into arrayof mapsin Groovy on the Server. (This would probably have to recurse through the response)

在服务器上的 Groovy中将上述内容转换为映射数组的最佳方法是什么。(这可能必须通过响应递归)

I am looking for something easy that doesn't include a lot of libraries. (I dont have maven)

我正在寻找一些简单的东西,不包括很多图书馆。(我没有maven)

采纳答案by tim_yates

EDIT: Groovy since 1.8.0 has an integrated JsonSlurper:

编辑:Groovy 从 1.8.0 开始集成了 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...

使用 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, listis a list of Maps, so if you just wanted a list of the names for example, you could just do:

正如你所看到的,list是一个地图列表,所以如果你只是想要一个名称列表,你可以这样做:

def names = list.name

To use this in your Gaelyk app, you should just need to download json-lib-2.3-jdk15.jar from hereand do something similar (without the @Grab then, as you'll have the jar in your WEB-INF/libfolder.

要在您的 Gaelyk 应用程序中使用它,您只需要从这里下载 json-lib-2.3-jdk15.jar并执行类似的操作(然后不使用 @Grab,因为您的WEB-INF/lib文件夹中将有 jar 。

--edit--

- 编辑 -

Looking around, found this page showing the dependenciesfor json-lib

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

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

测试脚本中的@Grab 为你做了很多后台工作

回答by Buhake Sindi

JSON Arrays starts with a [character and ends with a ]character. JSON object starts with a {and ends with }.

JSON 数组以[字符开头并以字符结尾]。JSON 对象以 a 开头{并以}.

If you go to JSON.org, you can download JSONArray.java. Use that to create a JSON array. You then loop through the array for (int i = 0; i < array.length(); i++)and retrieve each JSON object by calling array.getJSONObject(i);which returns JSONObject. From there, get the respective attribute value, e.g. long uid = json.getLong("uid");

如果你去 JSON.org,你可以下载 JSONArray.java。使用它来创建一个 JSON 数组。然后循环遍历数组for (int i = 0; i < array.length(); i++)并通过调用array.getJSONObject(i);which 返回来检索每个 JSON 对象JSONObject。从那里,获取相应的属性值,例如long uid = json.getLong("uid");

Hope this helps.

希望这可以帮助。