使用 JSONPath 从 JSON 对象获取单个值

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

Getting a single value from a JSON object using JSONPath

jsonjsonpath

提问by Ilija

I have the following JSON and I need to get the plain namevalue using JSONPath:

我有以下 JSON,我需要name使用JSONPath获取纯值:

{
  "single" : {
    "id" : 1, 
    "name" : "Item name"
  }
}

Expression that I used is $.single.namebut I always get an array:

我使用的表达式是$.single.name但我总是得到一个数组:

[ "Item name" ]

instead of a string value ("Item name").

而不是字符串值 ( "Item name")。

采纳答案by Tim

but I always get an array:

但我总是得到一个数组:

That is meant to happen. As you can read in this documentation, under 'Result' (almost at the bottom):

那是注定要发生的。正如您在本文档中所读到的,在“结果”下(几乎在底部):

Please note, that the return value of jsonPath is an array, which is also a valid JSON structure. So you might want to apply jsonPath to the resulting structure again or use one of your favorite array methods as sort with it.

请注意,jsonPath 的返回值是一个数组,也是一个有效的 JSON 结构。因此,您可能希望再次将 jsonPath 应用于结果结构,或者使用您最喜欢的数组方法之一对其进行排序。

So basically it will always return an array. If you need the data as an other type, e.g. a String in this case, you will have to do the conversion yourself I'm afraid.

所以基本上它总是会返回一个数组。如果您需要数据作为其他类型,例如在这种情况下是字符串,恐怕您将不得不自己进行转换。

回答by pepan

I was using the Java implementationof JSONPath and got to the very same issue. What worked for me was to add '[0]' to the json path string. So in your case:

我正在使用JSONPath的Java 实现并遇到了同样的问题。对我有用的是将 '[0]' 添加到 json 路径字符串中。所以在你的情况下:

$.single.name[0]

$.single.name[0]

回答by R.G

I know this is an old question , but the following worked for me when writing unit test for a spring boot application. Hope this helps someone in a similar situation.

我知道这是一个老问题,但是在为 Spring Boot 应用程序编写单元测试时,以下内容对我有用。希望这可以帮助处于类似情况的人。

As Tim Castelijns mentioned , the result is always an array.

正如 Tim Castelijns 提到的,结果总是一个数组。

Sample Json

示例 Json

{
  "validationErrors": [
    {
      "field": "location",
      "message": "must not be null"
    },
    {
      "field": "address",
      "message": "must not be blank"
    },
    {
      "field": "id",
      "message": "must be null"
    },
    {
      "field": "name",
      "message": "must not be blank"
    }
  ]
}

and the unit test part

和单元测试部分

//verify if the id is validated for null
andExpect((ResultMatcher) jsonPath("$.validationErrors[?(@.field=='id')].message").isArray()).
andExpect((ResultMatcher) jsonPath("$.validationErrors[?(@.field=='id')].message",hasItem("must be null"))).

回答by JRichardsz

I think, it depends of language implementation.

我认为,这取决于语言实现。

For example in nodejs there is a npm module : https://www.npmjs.com/package/jsonpath

例如在 nodejs 中有一个 npm 模块:https://www.npmjs.com/package/jsonpath

Which have a method called value, which does exactly what we need

其中有一个名为value的方法,它完全符合我们的需要

jp.value(obj, pathExpression[, newValue])

jp.value(obj, pathExpression[, newValue])

Returns the value of the first element matching pathExpression. If newValue is provided, sets the value of the first matching element and returns the new value.

返回匹配 pathExpression 的第一个元素的值。如果提供了 newValue,则设置第一个匹配元素的值并返回新值。

I tried it and worked!

我试过了,成功了!