背景
因为之前用到Jmeter的Json 提取器涉及到JsonPath的使用,因此查找了一些文章,将里面的精华部分运算符的使用提取出来记录以下,方便自己以后使用。
JsonPath
- JsonPath参照XPath解析xml的方式来解析Json
- JsonPath用符号$表示最外层对象,类似于Xpath中的根元素
- JsonPath可以通过点语法来检索数据,如:
shell $.store.book[0].title - 也可以使用中括号[]的形式,如
shell $['store']['book'][0]['title']
运算符(Operators)
|
|---|
JsonPath案例
{
"lemon": {
"teachers": [{
"id": "101",
"name": "华华",
"addr": "湖南长沙",
"age": 25
},
{
"id": "102",
"name": "韬哥",
"age": 28
},
{
"id": "103",
"name": "Happy",
"addr": "广东深圳",
"age": 16
},
{
"id": "104",
"name": "歪歪",
"addr": "广东广州",
"age": 29
}
],
"salesmans": [{
"id": "105",
"name": "毛毛",
"age": 17
},
{
"id": "106",
"name": "大树",
"age": 27
}
]
},
"avg": 25
} JsonPath例子及说明
| JsonPath | 路径说明 |
|---|---|
| $.lemon.teachers[*].name | 获取所有老师的的名称 |
| $..name | 获取所有人的名称 |
| $.lemon.* | 所有的老师和销售 |
| $.lemon..age | 所有人的年龄 |
| $..age | 所有人的年龄 |
| $.lemon.teachers[*].age | 所有老师的年龄 |
| $.lemon.teachers[3] | 索引为3(第4个)老师的信息 |
| $..teachers[3] | 索引为3(第4个)老师的信息 |
| $.lemon.teachers[-2] | 倒数第2个老师的信息 |
| $..teachers[-2] | 倒数第2个老师的信息 |
| $..teachers[1,2] | 第2到第3个老师的信息 |
| $..teachers[:2] | 索引0(包含)到索引2(不包含)的老师信息 |
| $..teachers[1:3] | 索引1(包含)到索引3(不包含)的老师信息 |
| $..teachers[-2:] | 最后的两个老师的信息 |
| $..teachers[2:] | 索引2开始的所有老师信息 |
| $..teachers[?(@.addr)] | 所有包含地址的老师信息(jsonpath_rw不支持) |
| $.lemon.teachers[?(@.age < 20)] | 所有年龄小于20的年龄信息(jsonpath_rw不支持) |
更多jsonpath_rw用法参考:
本文标题:JsonPath 运算符使用
本文链接:https://blog.quwenai.cn/post/2829.html
版权声明:本文不使用任何协议授权,您可以任何形式自由转载或使用。






还没有评论,来说两句吧...