测试过程中会遇到一些复杂的json响应。比如多层list嵌套时的取值的问题。本篇主要讲述如何运用JsonPath表达式来解决多层嵌套取值的问题。
语法参考 https://goessner.net/articles/JsonPath/
https://easy-mock.com/mock/5d5128dd7d20f4574035dc71/example/etc_mock
{
"response": [
{
"total": 4
},
{
"location": "Beijing",
"detail": {
"area": [
{
"date": "2018-06",
"city": "北京"
}
],
"province": "北京",
"type": "domestic",
"operator": "联通"
}
},
{
"location": "Shanghai",
"detail": {
"area": [
{
"date": "2018-11",
"city": "上海"
}
],
"province": "上海",
"type": "domestic",
"operator": "联通"
}
},
{
"location": "Hangzhou",
"detail": {
"area": [
{
"date": "2019-04",
"city": "杭州"
}
],
"province": "杭州",
"type": "domestic",
"operator": "联通"
}
},
{
"location": "Hangzhou",
"detail": {
"area": [
{
"date": "2019-04",
"city": "杭州"
}
],
"province": "杭州",
"type": "domestic",
"operator": "联通"
}
}
],
"responseHeader": {
"time": 1561284795013,
"version": "V1.1.0",
"status": 200
}
}
$..city:列出所有省份
..response[1].detail 提取response中第二个list中的detail
$..[?(@.location=="Shanghai")] 筛选出location为Shanghai的所有list数据
$..response[:3] 筛选前三条数据
注意:..response[:3] 是筛选出前三条数据 ;..response[3] 是排除前三条数据;