【python】接口自动化测试中,json解析神器jsonpath
通常做接口自动化测试时,需要对接口返回的数据转成字典类型,然后对字典中的数据进行取值,当层级越来越深的时候取值就会变得困难
这时就体现出jsonpath的强大之处了,首先进行安装
pip install jsonpath
示例:
import jsonpath result = { "name": "中国", "province": [{ "name": "黑龙江", "cities": { "city": ["哈尔滨", "大庆"] } }, { "name": "广东", "cities": { "city": ["广州", "深圳", "珠海"] } }, { "name": "台湾", "cities": { "city": ["台北", "高雄"] } }, { "name": "新疆", "cities": { "city": ["乌鲁木齐"] } }] } name1 = jsonpath.jsonpath(result,'$.name') name = jsonpath.jsonpath(result,'$..name') city = jsonpath.jsonpath(result,'$..cities') print(name1) print(name) print(city)
输出的结果为
jsonpath语法与对应的xpath