python自动化-题取参数

# -*- coding:utf-8 -*-
import jsonpath
class_info = {"class_one": {
"students": [
{"name": "张一",
"sex": "男",
"age": 18,
"height": 170.5
},
{"name": "张二",
"sex": "女",
"age": 20,
"height": 160.5
},
{"name": "张三",
"sex": "男",
"age": 18,
"height": 170.5
},
{"name": "张四",
"sex": "女",
"age": 18,
"height": 165.5
},
],
"teacher": {
"name": "李小二",
"sex": "男",
"age": 30,
"height": 185.5,
"teacher": "递归搜索测试"
}
}
}
$ —根节点(root)
. —子节点(一个点)
… —子孙节点(两个点) 递归搜索
(*):—通配符,表示所有的元素
[] —子节点
1、题取整个name

res = jsonpath.jsonpath(class_info,"$..name")

res = jsonpath.jsonpath(class_info,"$.[name]")
获取最后一个名称:
print(res[-1])

参考:
https://www.icode9.com/content-1-1297057.html

从接口自动化返回的需要转为json格式

response_text: {'code': 0, 'msg': 'success', 'data': {'ticket_info': [{'ticket_id': '202209071816fD47sxdG9605', 'ticket_name': '免费票'}, {'ticket_i
d': '202209071816qRDKJyuG9432', 'ticket_name': 'VIP'}], 'activity_id': 'activity_202209071816Gagle1in8650'}, 'forward_url': ''}

res = jsonpath.jsonpath(rep.json(), "$.[ticket_id]")  获取活动票id

其次 获取msg的字段:

res = jsonpath.jsonpath(rep.json(), "$[msg]")


 

posted @ 2022-09-07 18:07  陈文芝  阅读(24)  评论(0编辑  收藏  举报