jmeter多个参数中取变量值
在查询的消息体内取变量的值,作为入参使用,如果返回的查询消息一个参数能唯一标识出来,那么直接取就行了,如果返回的参数不能唯一标识出来,那么就需要预处理了,本文对这两种情况进行说明举例。
一、能唯一标识的参数取值
二、当变量不能唯一标识,就不好用正则表达式进行取值,那么需要用到预置条件BeanShell PostProcesser
import org.json.simple.JSONValue; //格式转化
import org.json.simple.JSONArray; //格式转化
import org.json.simple.JSONObject; //格式转化
String jsonVars= prev.getResponseDataAsString(); //返回变量转成文本格式赋值给变量
Object obj=JSONValue.parse(jsonVars);
JSONArray jsonArray = (JSONArray)obj;
JSONObject jsonObject = (JSONObject)jsonArray.get(jsonArray.size()-1); //矩阵中关键参数的个数
int len=jsonArray.size();
for (int i=0; i < len; i++){
JSONObject jsonObject=(JSONObject)jsonArray.get(i);
if(jsonObject.get("serviceName").toString().equals("用户充值")) //满足取值条件,等于“用户充值
{
log.info(jsonObject.get("serviceName"));
vars.put("betaservice_id",jsonObject.get("betaServiceID"));
vars.put("betaactivity_id",jsonObject.get("betaActivityID"));
log.info("betaservice_id: "+vars.get("betaservice_id"));
log.info("betaactivity_id: "+vars.get("betaactivity_id"));
}
}
三、多层中怎么取参数值,比方说在安全组中去安全组id
import org.json.simple.JSONValue;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
String jsonSubnets= prev.getResponseDataAsString();
Object obj=JSONValue.parse(jsonSubnets);
JSONObject jsonObject = (JSONObject)obj;
JSONArray jsonArray = (JSONArray)jsonObject.get("security_groups");
vars.putObject("sgs",jsonArray);
vars.put("sg_count",Integer.toString(jsonArray.size()));
int len=jsonArray.size();
vars.put("isSg","false");
for(int i=0; i <len; i++)
{
JSONObject jsonObject=(JSONObject)jsonArray.get(i);
if(jsonObject.get("vpc_id").toString().equals(vars.get("vpc_id")))
{
log.info(jsonObject.get("vpc_id"));
vars.put("sg_id",jsonObject.get("id").toString());
vars.put("isSg","true");
}
}
查询安全组返回的消息内容
{"security_groups":[{"id":"3583a3f8-25c1-4dde-b1d5-baf699fd1be9","name":"default","vpc_id":"default","security_group_rules":[{"direction":"ingress","ethertype":"IPv4","id":"aabeeb56-eb05-46d7-bb0a-a9f46fccdaaa","remote_group_id":"3583a3f8-25c1-4dde-b1d5-baf699fd1be9","security_group_id":"3583a3f8-25c1-4dde-b1d5-baf699fd1be9"},{"direction":"egress","ethertype":"IPv4","id":"d2c3db7c-b45b-4236-8630-88988b039ca8","security_group_id":"3583a3f8-25c1-4dde-b1d5-baf699fd1be9"},{"direction":"ingress","ethertype":"IPv6","id":"06d9fb57-4505-4c01-b75f-1a6975063a4e","remote_group_id":"3583a3f8-25c1-4dde-b1d5-baf699fd1be9","security_group_id":"3583a3f8-25c1-4dde-b1d5-baf699fd1be9"},{"direction":"egress","ethertype":"IPv6","id":"0366b761-8b46-44ba-9892-0b0011d428d3","security_group_id":"3583a3f8-25c1-4dde-b1d5-baf699fd1be9"}]},{"id":"c8a6b4b8-3257-4e9f-9966-63278a8c2660","name":"default","vpc_id":"fafc90e1-99ac-4e31-9b3a-87d7a007ff2d","security_group_rules":[{"direction":"egress","ethertype":"IPv4","id":"d33b699a-0d8b-4ff3-a12c-ec6c0d6e410a","security_group_id":"c8a6b4b8-3257-4e9f-9966-63278a8c2660"},{"direction":"ingress","ethertype":"IPv4","id":"d0a38e0c-689b-446a-a3bc-8252386870e4","remote_group_id":"c8a6b4b8-3257-4e9f-9966-63278a8c2660","security_group_id":"c8a6b4b8-3257-4e9f-9966-63278a8c2660"}]}]}
- 点赞
- 收藏
- 关注作者
评论(0)