MeterSphere使用Beanshell后置脚本获取Json响应数据作为下一个接口参数
2021/7/14 7:07:02
本文主要是介绍MeterSphere使用Beanshell后置脚本获取Json响应数据作为下一个接口参数,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
MeterSphere使用后置脚本Beanshell获取Json中的数据(MeterShpere的beanshell可参考jmeter)
前提:
因新增用户需要用到角色id,但列表中的角色很多,需要查到对应的角色名称获得对应的角色id
解决方法:通过Beanshell获取json数据,并比对角色名称获得角色id
该查询角色请求接口返回的响应体:
{ "code": 0, "data": { "totalCount": 23, "pageSize": 10, "totalPage": 3, "currPage": 1, "list": [ { "roleId": "1386932755329212418", "roleName": "rolename68683436", "roleDescribe": "角色描述", "createTime": 1619505511000, "permissionList": null, "permissionIdList": null }, { "roleId": "1386933252324876289", "roleName": "rolename12237116", "roleDescribe": "角色描述", "createTime": 1619505630000, "permissionList": null, "permissionIdList": null }, { "roleId": "1386937586399862786", "roleName": "rolename30616863", "roleDescribe": "角色描述", "createTime": 1619506663000, "permissionList": null, "permissionIdList": null }, { "roleId": "1386937804906323970", "roleName": "rolename59824176", "roleDescribe": "角色描述", "createTime": 1619506715000, "permissionList": null, "permissionIdList": null }, { "roleId": "1386938077972291586", "roleName": "rolename38355327", "roleDescribe": "角色描述", "createTime": 1619506781000, "permissionList": null, "permissionIdList": null }, { "roleId": "1386938538154549249", "roleName": "rolename71155716", "roleDescribe": "角色描述", "createTime": 1619506890000, "permissionList": null, "permissionIdList": null }, { "roleId": "1386938671428558850", "roleName": "rolename76969316", "roleDescribe": "角色描述", "createTime": 1619506922000, "permissionList": null, "permissionIdList": null }, { "roleId": "1386938740525522945", "roleName": "rolename52641082", "roleDescribe": "角色描述", "createTime": 1619506938000, "permissionList": null, "permissionIdList": null }, { "roleId": "1386938875733106689", "roleName": "rolename62736553", "roleDescribe": "角色描述", "createTime": 1619506971000, "permissionList": null, "permissionIdList": null }, { "roleId": "1386939085746102274", "roleName": "rolename79351680", "roleDescribe": "角色描述", "createTime": 1619507021000, "permissionList": null, "permissionIdList": null } ] }
后置脚本获取json
操作步骤:
1、beanshell脚本中获得其他变量:
String add_rolename=vars.get("rolename");
2、通过beanshell后置脚本找到对应角色名称,例如:rolename的角色id,并输出变量给其他接口使用
vars.put("roleId", roleId);
//导入json包 import org.json.*; //其他变量赋值到beanshell String add_rolename=vars.get("rolename"); //log.info("--------------" + add_rolename); //获取接口响应数据 String response_data = prev.getResponseDataAsString(); //log.info(response_data); //转换为Object对象类型 JSONObject data_obj = new JSONObject(response_data); //log.info("--------------" + data_obj.toString()); //获取数据 String StrData = data_obj.get("data").toString(); //log.info("--------------" + StrData.toString()); //转换为Object对象类型 JSONObject data2_obj = new JSONObject(StrData); //获取list数据 JSONArray list_array = data2_obj.getJSONArray("list"); log.info("--------------" + list_array.toString()); int len = list_array.length(); String strlen = Integer.toString(len); for(int i=0;i < len;i++) { JSONObject jsonTemp = (JSONObject)list_array.getJSONObject(i); //log.info("--------------" + jsonTemp.getString("roleName")); String roleName=jsonTemp.getString("roleName"); if (roleName.equals(add_rolename)){ String roleId=jsonTemp.getString("roleId"); log.info("--------------" + roleId); vars.put("roleId", roleId); } }
控制台输出结果:
获得的角色id:roleId在其他接口中的使用:
{ "trueName": "${true_name}", "roleIdList": [ "${roleId}" ], "organizationCodeArr": [ "0" ], "organizationCode": "0", "loginPassword": "${userPwd}", "mobile": "${mobile}", "userName": "${userName}", "userDescribe": "用户描述", "email": "${email}" }
原文:https://www.www.zyiz.net/content-3-946886.html
这篇关于MeterSphere使用Beanshell后置脚本获取Json响应数据作为下一个接口参数的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-21Vue3教程:新手入门到实践应用
- 2024-12-21VueRouter4教程:从入门到实践
- 2024-12-20Vue3项目实战:从入门到上手
- 2024-12-20Vue3项目实战:新手入门教程
- 2024-12-20VueRouter4项目实战:新手入门教程
- 2024-12-20如何实现JDBC和jsp的关系?-icode9专业技术文章分享
- 2024-12-20Vue项目中实现TagsView标签栏导航的简单教程
- 2024-12-20Vue3入门教程:从零开始搭建你的第一个Vue3项目
- 2024-12-20从零开始学习vueRouter4:基础教程
- 2024-12-20Vuex4课程:新手入门到上手实战全攻略