java - 如何使用 JSONPath 获取根级别 JSON 对象的键/名称?

标签 java json jsonpath

所以我一直在用jayway JSONPath像下面这样查询 JSON 对象:

{
  "messageHeader" : {

     "sentBy" : "someOne",
     "userName" : "John Doe"
  },

 "payload" : []
}

这在大多数情况下工作正常,除了现在我希望使用路径 $..* 选择根级对象并根据消息中存在的对象类型使用它们的键/名称执行单独的任务作为标识符。 但是,使用上述路径或 $.* ,将始终生成一个 JSONArray,就像这样:

[{sentBy:someOne,userName:John Doe},[]]

JSON 对象似乎是匿名的,它们没有 key 。无论如何我可以直接将这些对象的键作为字符串访问吗?如果数据不存在,那么为什么路径: $.messageHeader.sentBy 有效?

最佳答案

来自 README JsonPath 的:

When evaluating a path you need to understand the concept of when a path is definite. A path is indefinite if it contains:

  • .. - a deep scan operator
  • ?() - an expression
  • [, (, )] - multiple array indexes

Indefinite paths always returns a list (as represented by current JsonProvider).

这应该可以解释上述现象。

关于java - 如何使用 JSONPath 获取根级别 JSON 对象的键/名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30878199/

相关文章:

javascript - AngularJS $http.get 异步执行顺序

.net - JSON Outline 工具未出现在 VS 2015 或 2017 中

java - 带有 IndexOutOfBoundsException 的递归

java - Play框架JSON列表绑定(bind)

json - JSON键是否必须用引号括起来?

java - 检查 JSON 是否包含具有值的节点

javascript - jsonpath : How to access a key in current object when the key name is having "hyphen" in it?

java - 如何将Weld集成到maven项目中?

java - Flex移动项目: using Blazeds to access data from java class

.net - 可以使用JSONPath搜索不区分大小写的内容吗?