• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

yxchun

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

Jmeter BeanShell, 读取HTTP请求返回的JSON,并将其存到文件中

1、创建BeanShell Sampler

将fastjson-1.2.30.jar放到 Jmeter安装目录\lib 下

 

import java.io.*;
import com.alibaba.fastjson.JSONObject;
import java.io.IOException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;


private static void dealJson() throws IOException {
	
// 获取上个http请求返回的json数据
String jsonString = prev.getResponseDataAsString();
// 将json转为JSONObject
JSONObject obj = JSONObject.parseObject(jsonString);
// 拿到响应码为200的请求
if(obj.getString("code").equals("200")){
//拿到data数据
	String data = obj.getString("data");
// 将data再次转为JSONObject
JSONObject parse = (JSONObject) JSONObject.parse(data);
// 获取filepath的数据
String result=parse.getString("filePath");
//数据保存的文件位置
String filepath = "D:/os/video/filepath.txt";

	try{
//FileWriter(,true),写成true代表写文件时追加到文件中;默认为false,是覆盖文件内容
		FileWriter fileWriter = new FileWriter(filepath,true);
//每次写入换行
	    fileWriter.write("\r\n");
//追加
         fileWriter.append(result);
         fileWriter.flush();
         fileWriter.close();
		}catch(IOException e){
      e.printStackTrace();
      }
	}
	}
//调用
dealJson();	

  

posted on 2023-09-13 15:43  yxchun  阅读(427)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3