jsonpath用法示例
时间: 2023-12-19 09:05:37 浏览: 61
假设我们有以下 JSON 数据:
```
{
"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
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
```
以下是 JSONPath 的一些示例用法:
- 获取所有书的作者:
```
$.store.book[*].author
```
- 获取第一本书的作者:
```
$.store.book[0].author
```
- 获取所有书中价格低于 10 的书:
```
$.store.book[?(@.price < 10)].title
```
- 获取所有分类为 fiction 的书的作者:
```
$.store.book[?(@.category == 'fiction')].author
```
- 获取红色自行车的价格:
```
$.store.bicycle.price
```
阅读全文