1:Json后置处理器提取结果作为下一个sampler的传入参数
1.1:【线程组】->【简单控制器】->【HTTP sampler】->【Beanshell后置取样器】->【查看结果树】
1.2:Beanshell后置取样器获取HTTP请求1所有响应数据returnData和其中单独的数据
log.info(returnData)可以在Log中输出returnData的值
1.4:添加第二个sampler
1.5:查看结果树 HTTP请求1的响应数据作为HTTP请求2的POST Data传入服务器
2:Beanshell对Json数据的增删改查
2.1:Beanshell后置处理器对响应数据的查询
2.1.1:eclipse的httpclient获取接口返回数据
Maven导入的Jar包
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
package Test.Json;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import com.alibaba.fastjson.JSONObject;
public class GetJsonData {
private String URL = "http://mobile.weather.com.cn/data/sk/101010100.html";
// Get方法获取 http://mobile.weather.com.cn/data/sk/101010100.html
@Test
public void GetMethodData() {
HttpGet httpGet = new HttpGet(URL);
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
CloseableHttpResponse response = httpClient.execute(httpGet);
int code = response.getStatusLine().getStatusCode();// 获取返回状态码
System.out.println(code);
String returnData = EntityUtils.toString(response.getEntity());// 获取接口返回的text格式数据
System.out.println(returnData);
//======================================================================
// 将text格式数据转换成JSON格式数据
JSONObject jsonObject = JSONObject.parseObject(returnData);
// sk_inf为Json的一个节点 节点下采薇key value键值对
JSONObject data = jsonObject.getJSONObject("sk_info");
System.out.println(data);
//======================================================================
//增
data.put("phone", 110);
System.out.println(data);
//======================================================================
//删除
data.remove("cityName");
//======================================================================
//改
data.put("phone", 120);
//======================================================================
//查询
data.get("phone");
} catch (Exception e) {
System.out.println(e);
}
}
}
2.1.2:Beanshell对响应Json数据的删除
Beanshell后置处理器脚本
//导入Json jar包
import com.alibaba.fastjson.JSONObject;
String returnData = prev.getResponseDataAsString();
log.info(returnData);
//将获取的数据放入 方便下面的sampler取值
JSONObject jsonObject = JSONObject.parseObject(returnData);
JSONObject data = jsonObject.getJSONObject("sk_info");
//eclipse里面不用toString Jmeter中需要
log.info(data.toString());//这里输出有“北京”
data.remove("cityName");
log.info(data.toString());//这里输出没有"北京"
vars.put("returnData",returnData);
vars.put("data",data.toString());//data不是string形式变量 需要toString转换
HTTP请求2
查看结果树
2.1.3:Beanshell对响应Json数据的增
//导入Json jar包
import com.alibaba.fastjson.JSONObject;
String returnData = prev.getResponseDataAsString();
log.info(returnData);
//将获取的数据放入 方便下面的sampler取值
JSONObject jsonObject = JSONObject.parseObject(returnData);
JSONObject data = jsonObject.getJSONObject("sk_info");
//eclipse里面不用toString Jmeter中需要
log.info(data.toString());
data.put("name","Anndy");
log.info(data.toString());//这里输出增加"name":"Anndy"
vars.put("returnData",returnData);
vars.put("data",data.toString());//data不是string形式变量 需要toString转换
2.1.4:Beanshell对响应Json数据的改
//导入Json jar包
import com.alibaba.fastjson.JSONObject;
String returnData = prev.getResponseDataAsString();
log.info(returnData);
//将获取的数据放入 方便下面的sampler取值
JSONObject jsonObject = JSONObject.parseObject(returnData);
JSONObject data = jsonObject.getJSONObject("sk_info");
//eclipse里面不用toString Jmeter中需要
log.info(data.toString());//cityName:北京
data.put("cityName","南京");
log.info(data.toString());//这里输出增加"cityName":"南京"
vars.put("returnData",returnData);
vars.put("data",data.toString());//data不是string形式变量 需要toString转换
2.1.5:Beanshell对响应Json数据的查
//导入Json jar包
import com.alibaba.fastjson.JSONObject;
String returnData = prev.getResponseDataAsString();
log.info(returnData);
//将获取的数据放入 方便下面的sampler取值
JSONObject jsonObject = JSONObject.parseObject(returnData);
JSONObject data = jsonObject.getJSONObject("sk_info");
//eclipse里面不用toString Jmeter中需要
log.info(data.toString());
String city = data.get("cityName");
vars.put("returnData",returnData);
vars.put("city",city);
2.1.6:放入list集合
//导入Json jar包
import com.alibaba.fastjson.JSONObject;
String returnData = prev.getResponseDataAsString();
log.info(returnData);
//将获取的数据放入 方便下面的sampler取值
JSONObject jsonObject = JSONObject.parseObject(returnData);
JSONObject data = jsonObject.getJSONObject("sk_info");
//eclipse里面不用toString Jmeter中需要
log.info(data.toString());
//beanshell不能使用泛型
List list = new ArrayList();
list.add("AAA");
list.add("BBB");
list.add("CCC");
data.put("nameList", list);
//vars只能放入基础类型数据
vars.put("data",data.toString());//data不是基础类型 所以需要转换 否则会报错
原文链接:https://blog.csdn.net/hujyhfwfh2/article/details/80714979