import re import json import random dict1 = {} def get_variable_value_from_result(v, res): # 可变参数想取多个值用;分割, 如:'${fleetName}=[data][mileageList][0][fleetName];${fleetName}=[data][oilList][1][fleetName]' var_list = v.split(';') for var in var_list: param = var.split('=') if len(param) == 2: # 判断可变参数是否有效 为空,或者不是以[开头,或者以]结尾的 if param[1] == '' or not re.search(r'^[', param[1]) or not re.search(r']$', param[1]): result = "关联参数设置错误" continue response_js = json.loads(res) value = "" # 通过等号后的键值,逐步获得所指向的json串中的值 for key in param[1][1:-1].split(']['): # 随机选择list中的某个值。excel写法如:[random@5] if 'random' in key: key = random.randint(0, int(key.split('@')[1])) # 按条件筛选值,如获取type为21的list值。excel写法如:[type:21] elif ':' in key: condition_key, condition_value = key.split(':')[0], key.split(':')[1] for index in range(len(response_js)): try: if str(response_js[index][condition_key]) == condition_value: key = index break except: continue # 通过键值获取结果中的相应字段值 try: value = response_js[int(key)] except: try: value = response_js[key] except: # 未找到时,传空值 value = None break response_js = value # 将关联参数和数值存到dict中 if value is not None: dict1[str(param[0])] = str(value) print(dict1) pass res = """ {"resultCode":200,"message":"OK","data":{"total":230,"page_total":23,"list":[{"id":822,"pmName":"测试152155","pmEveRange":"","pmFrdUser":111,"pmGroupRewardUser":1111,"pmRewardUpperlimitcntUser":1111,"pmFriendsRewardSalesman":1111,"pmGroupRewardSalesman":1111,"pmRewardUpperlimitcntSalesman":11111,"createUser":"testInter","createTime":"2021-01-18 15:21:49","pmStatus":1,"pmStatusStr":"有效","operateStatus":1},{"id":821,"pmName":"测试152114","pmEffectiveRange":"","pmFriendsRewardUser":111,"pmGroupRewardUser":1111,"pmRcntUser":1111,"pmesman":1111,"pmGrosman":1111,"pmRsman":11111,"createUser":"testInter","createTime":"2021-01-18 15:21:08","pmStatus":1,"pmStatusStr":"有效","operateStatus":1},{"id":820,"pmName":"测试151907","pmEffectiveRange":"","pmFriendsRewardUser":111,"pmGroupRewardUser":1111,"pmRewardUpperlimitcntUser":1111,"pmFriendsRewardSalesman":1111,"pmGroupRewardSalesman":1111,"pmRewardUpperlimitcntSalesman":11111,"createUser":"testInter","createTime":"2021-01-18 15:19:01","pmStatus":1,"pmStatusStr":"有效","opeatus":1},{"id":819,"pmName":"测试151314","pmEffectiveRange":"","pmFriendsRewardUser":111,"pmGroupRewardUser":1111,"pmRewardUpperlimitcntUser":1111,"pmFriendsRewardSalesman":1111,"pmGroupRewardSalesman":1111,"pmRewardUpperlimitcntSalesman":11111,"createUser":"testInter","createTime":"2021-01-18 15:13:09","pmStatus":1,"pmStatusStr":"有效","operateStatus":1},{"id":817,"pmName":"测试143541","pmEffectiveRange":"","pmFriendsRewardUser":111,"pmGroupRewardUser":1111,"pmRewardUpperlimitcntUser":1111,"pmFriendsRewardSalesman":1111,"pmGroupRewardSalesman":1111,"pmRewardUpperlimitcntSalesman":11111,"createUser":"testInter","createTime":"2021-01-18 14:35:35","pmStatus":1,"pmStatusStr":"有效","operateStatus":1},{"id":816,"pmName":"测试143250","pmEffectiveRange":"","pmFriendsRewardUser":111,"pmGroupRewardUser":1111,"pmRewardUpperlimitcntUser":1111,"pmFriendsRewardSalesman":1111,"pmGroupRewardSalesman":1111,"pmRewardUpperlimitcntSalesman":11111,"createUser":"testInter","createTime":"2021-01-18 14:32:44","pmStatus":1,"pmStatusStr":"有效","operateStatus":1},{"id":813,"pmName":"测试141737","pmEange":"","pmFriendsUser":111,"pmGrUser":1111,"pmRewtcntUser":1111,"pmSalesman":1111,"pmGroupRewardSalesman":1111,"pmRewardUpperlimitcntSalesman":11111,"createUser":"testInter","createTime":"2021-01-18 14:17:31","pmStatus":1,"pmStatusStr":"有效","operateStatus":1},{"id":812,"pmName":"测试141709","pmEffectiveRange":"","pmFridUser":111,"pmardUser":1111,"pmRewardUpperlimitcntUser":1111,"pmFrdSalesman":1111,"pmGrouman":1111,"pmRewarlesman":11111,"createUser":"testInter","createTime":"2021-01-18 14:17:03","pmStatus":1,"pmStatusStr":"有效","operateStatus":1},{"id":811,"pmName":"测试141524","pmEffectiveRange":"","pmFriendsRewardUser":111,"pmGroupRewardUser":1111,"pmRewtcntUser":1111,"pmFriendlesman":1111,"pmGresman":1111,"pmRewarlesman":11111,"createUser":"testInter","createTime":"2021-01-18 14:15:18","pmStatus":1,"pmStatusStr":"有效","operateStatus":1},{"id":810,"pmName":"测试141423","pmEffectiveRange":"","pmFriendsRewardUser":111,"pmGroupRewardUser":1111,"pmRewardUpperlimitcntUser":1111,"pmman":1111,"pmGrolesman":1111,"pmRewardUpperlimitcntSalesman":11111,"createUser":"testInter","createTime":"2021-01-18 14:14:17","pmStatus":1,"pmSsStr":"有效","operatus":1}]}} """ # v = "${fleetName}=[data][list][0][id]" # v = "${fleetName}=[data][list][id:819][pmName]" # v = '${fleetName}=[data][list][pmName:测试151314][id]' v = '${fleetName}=[data][list][random@5][id]' get_variable_value_from_result(v, res)