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://blog.csdn.net/silencemylove/article/details/51373873
https://blog.csdn.net/weixin_46036165/article/details/104312325
https://www.cnblogs.com/jiachangwei/p/12142712.html