sailxu00 2020-06-21
json {"status":1,"msg":"登录成功"}
json {"status":1,"code":"10001","data":[{"id":1,"investId":"1","createTime":"2018-04-27 12:24:01","terms":"1","unfinishedInterest":"1.0","unfinishedPrincipal":"0","repaymentDate":"2018-05-27 12:24:01","actualRepaymentDate":null,"status":"0"},{"id":2,"investId":"1","createTime":"2018-04-27 12:24:01","terms":"2","unfinishedInterest":"1.0","unfinishedPrincipal":"0","repaymentDate":"2018-06-27 12:24:01","actualRepaymentDate":null,"status":"0"},{"id":3,"investId":"1","createTime":"2018-04-27 12:24:01","terms":"3","unfinishedInterest":"1.0","unfinishedPrincipal":"100.00","repaymentDate":"2018-07-27 12:24:01","actualRepaymentDate":null,"status":"0"}],"msg":"获取信息成功"}
上面的json结构嵌套了很多信息,完整的匹配几乎不可能成功。比如其中的createTime信息,根据执行接口测试用例的时间每次都不一样。同时这个时间是响应结果中较为次要的信息,在进行接口自动化测试时,是可以选择被忽略的。JsonPath可以完美解决上面的痛点。通过JsonPath可以从多层嵌套的Json中解析出所需要的值。
shell $.store.book[0].title
shell $[‘store‘][‘book‘][0][‘title‘]
运算符 | 说明 |
---|---|
$ | 根元素 |
@ | 当前元素 |
* | 通配符,可以表示任何元素 |
.. | 递归搜索 |
. | 子节点(元素) |
[‘‘ (, ‘‘)] | 一个或者多个子节点 |
[ (, )] | 一个或者多个数组下标 |
[start:end] | 数组片段,区间为[start,end) |
[?()] | 过滤器表达式,其中表达式结果必须是boolean类型,如可以是比较表达式或者逻辑表达式 |
{ "xdf_company": { "teachers": [ { "id": "101", "name": "老王", "addr": "北京海淀", "age": 25 }, { "id": "102", "name": "老李", "age": 28 }, { "id": "103", "name": "老刘", "addr": "山东济南", "age": 16 }, { "id": "104", "name": "老史", "addr": "山东青岛", "age": 29 } ], "salesmans": [ { "id": "105", "name": "老高", "age": 17 }, { "id": "106", "name": "老范", "age": 27 } ] }, "avg": 25 }
JsonPath | 路径说明 |
---|---|
$.xdf_company.teachers[*].name | 获取所有老师的的名称 |
$..name | 获取所有人的名称 |
$.xdf_company.* | 所有的老师和销售 |
$.xdf_company..age | 所有人的年龄 |
$..age | 所有人的年龄 |
$.xdf_company.teachers[*].age | 所有老师的年龄 |
$.xdf_company.teachers[3] | 索引为3(第4个)老师的信息 |
$..teachers[3] | 索引为3(第4个)老师的信息 |
$.xdf_company.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不支持) |
$.xdf_company.teachers[?(@.age < 20)] | 所有年龄小于20的年龄信息(jsonpath_rw不支持) |
pip install jsonpath==0.75
# 1:导入相关模块 import json import jsonpath # 2: 准备json字符串 jsonStr = ‘‘‘ {
"xdf_company": { "teachers": [ { "id": "101", "name": "老王", "addr": "北京海淀", "age": 25 }, { "id": "102", "name": "老李", "age": 28 }, { "id": "103", "name": "老刘", "addr": "山东济南", "age": 16 }, { "id": "104", "name": "老史", "addr": "山东青岛", "age": 29 } ], "salesmans": [ { "id": "105", "name": "老高", "age": 17 }, { "id": "106", "name": "老范", "age": 27 } ] }, "avg": 25 }
‘‘‘# 3:加载json字符串为json对象 json_obj = json.loads(jsonStr) # 4:使用jsonpath模块的jsonpath方法提取信息 # eg1: 提取所有包含addr属性的老师信息,结果为list类型 results = jsonpath.jsonpath(json_obj,"$..teachers[?(@.addr)]") print(results) # 输出结果:[{‘id‘: ‘101‘, ‘name‘: ‘老王‘, ‘addr‘: ‘北京海淀‘, ‘age‘: 25}, {‘id‘: ‘103‘, ‘name‘: ‘老刘‘, ‘addr‘: ‘山东济南‘, ‘age‘: 16}, {‘id‘: ‘104‘, ‘name‘: ‘老史‘, ‘addr‘: ‘山东青岛‘, ‘age‘: 29}] # eg2:提取所有年龄小于20岁的老师的name,结果为list类型 results2 = jsonpath.jsonpath(json_obj,"$.xdf_company.teachers[?(@.age < 20)].name") print(results2) # 输出结果为:[‘老刘‘]
pip install jsonpath-rw
# 1:导入相关模块 import json from jsonpath_rw import jsonpath, parse # 2: 准备json字符串 jsonStr = ‘‘‘ # 同上(略) ‘‘‘ # 3:加载为json对象 json_obj = json.loads(jsonStr) # 4:采用parse创建jsonpath对象(该案例是得到所有的老师name) jsonpath_expr = parse(‘$.xdf_company.teachers[*].name‘) # 5:通过jsonPath检索json后返回匹配的数据,类型是DatumInContext的list datumInContexts = jsonpath_expr.find(json_obj) # 采用列表推导式检索出所有匹配的值 values = [datum.value for datum in datumInContexts] print(values) # 输出结果为:[‘老王‘, ‘老李‘, ‘老刘‘, ‘老史‘] # 案例2:提取索引为4的老师的name jsonpath_expr = parse(‘$.xdf_company.teachers[3].name‘)datumInContexts = jsonpath_expr.find(json_obj) print(datumInContexts) values = [datum.value for datum in datumInContexts] print(values) # 结果为:[‘老刘‘]
{ "xdf_company": { "teachers": [ { "id": "101", "name": "老王", "addr": "北京海淀", "age": 25 }, { "id": "102", "name": "老李", "age": 28 }, { "id": "103", "name": "老刘", "addr": "山东济南", "age": 16 }, { "id": "104", "name": "老史", "addr": "山东青岛", "age": 29 } ], "salesmans": [ { "id": "105", "name": "老高", "age": 17 }, { "id": "106", "name": "老范", "age": 27 } ] }, "avg": 25 }
xdf_company