使用 JSONPATH 解析 JSON 数组文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9291435/
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-09-03 18:11:23  来源:igfitidea点击:

Parse JSON array file with JSONPATH

arraysjsonjsonpath

提问by Kheiri Selmi

I want to parse this with JSONPath:

我想用 JSONPath 解析这个:

[
  [50.4154134372953,-1.28486558931069,"CLASS B",9,205,0,"UK",431500382,3,4],
  [50.3058858494047,-0.976070494820637,"CLASS B",9,239,0,"UK",2750350,21,2]
]

Can you help with that please?

你能帮忙吗?

回答by ArjunShankar

If the object is:

如果对象是:

[
  [50.4154134372953,-1.28486558931069,"CLASS B",9,205,0,"UK",431500382,3,4],
  [50.3058858494047,-0.976070494820637,"CLASS B",9,239,0,"UK",2750350,21,2]
]

Then "$[0]"will return:

然后"$[0]"会返回:

[50.4154134372953,-1.28486558931069,"CLASS B",9,205,0,"UK",431500382,3,4]

And "$[1]"will return:

"$[1]"将返回:

[50.3058858494047,-0.976070494820637,"CLASS B",9,239,0,"UK",2750350,21,2]

You can do it two levels deep as well. "$[0][4]"will return:

你也可以做两层深。"$[0][4]"将返回:

205

You can also extract the elements of the array into a list with "$[*]", which will return a list of 2 elements. The first being:

您还可以使用 将数组的元素提取到一个列表中"$[*]",这将返回一个包含 2 个元素的列表。第一个是:

[50.4154134372953,-1.28486558931069,"CLASS B",9,205,0,"UK",431500382,3,4]

and the second being:

第二个是:

[50.3058858494047,-0.976070494820637,"CLASS B",9,239,0,"UK",2750350,21,2]

回答by Hakan Bilgin

Using DefiantJS, you can search a JSON structure with XPath syntax. This library extends the global object JSON with a search function.

使用 DefiantJS,您可以使用 XPath 语法搜索 JSON 结构。该库使用搜索功能扩展了全局对象 JSON。

In this scenario, you can write something like this;

在这种情况下,您可以这样写;

var data = [
  [50.4154134372953,-1.28486558931069,"CLASS B",9,205,0,"UK",431500382,3,4],
  [50.3058858494047,-0.976070494820637,"CLASS B",9,239,0,"UK",2750350,21,2]
],
search = JSON.search( data, '//*/*/*' );

Check out this fiddle; http://jsfiddle.net/hbi99/5NfeM/

看看这个小提琴;http://jsfiddle.net/hbi99/5NfeM/

回答by Jayen Chondigara

This works for me

这对我有用

JsonPath.with(jsonResponse).param("name", "getName").get("findAll { a -> a.name == name  }")