如何使用 JsonPath 访问 JSON 数组响应?

How to access a JSON Array response using JsonPath?

当 REST 服务响应作为 Json 数组返回时如何处理:例如:

[{"boolField":true,"intField":991, "StringField":"SampleString"},
 {"boolField":false, "intField":998, "StringField": "SampleString2"}]";

我看到的所有博客和例子都没有涉及上述场景。例如,我查看了 http://goessner.net/articles/JsonPath/ 表达式语法,但找不到解决方案。

例如下面的json,如果我想得到所有的作者,我可以使用$.store.book[*].author或者$..author

{ "store": {
    "book": [ 
      { "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      { "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      }]}
}

但是,如果像下面这样返回 REST 服务响应,那么如何从列表中获取所有作者

[
  { "category": "reference",
    "author": "Nigel Rees",
    "title": "Sayings of the Century",
    "price": 8.95
  },
  { "category": "fiction",
    "author": "Evelyn Waugh",
    "title": "Sword of Honour",
    "price": 12.99
   }
]

$..author 将检索所有作者。

通常,当您的根对象是一个数组时,您可以将 $ 视为一个数组,并执行 $[0] 之类的操作来检查第一项(您仍然会得到一个数组然而回来了)。

在我的例子中 [0].author 有效(我省略了 $)。