方式一(Jmeter BeanShell采样器提取接口)

接口获取response数据:

{"cost_time":0.15606689453125,"emotion_class":{"label":"hope","score":0.2862275540828705},"entity":null,"entity_class":{"label":"plants","score":0.2635917663574219},"keywords":"fruit"}

jmeter 线程组-JSR223 Sampler

1.png.

请输入图片描述

   //导入json包
import org.json.JSONObject;
import org.json.JSONArray;
import org.apache.jmeter.samplers.SampleResult
SampleResult rsp =ctx.getPreviousResult()       
String response_data = rsp.getResponseDataAsString()  //获取上一个接口返回数据,此处是String类型
log.info("\n" + response_data)
//将String类型的返回值构造成JSONObject对象
JSONObject jsonstring=new JSONObject(response_data);  //放进json对象里
String cost_time=jsonstring.get("cost_time").toString();        //取出cost_time对应的value值
String score=jsonstring.get("emotion_class").get("score").toString();        //取出score对应的value值
vars.put("cost_time",cost_time);
vars.put("score",score);
log.info(cost_time);
log.info(score);
//写入文件
FileWriter fstream=new FileWriter("E:/gongju/jmeter/pc.csv",true);//本地存储文件路径
BufferedWriter out = new BufferedWriter(fstream);
out.write(cost_time);//提取出来的cost_time
out.write(",");//换列
out.write(score);//提取出来的score
out.write(System.getProperty("line.separator"));//换行
out.close();
fstream.close();
最后修改:2024 年 01 月 24 日
如果觉得我的文章对你有用,请随意赞赏