在使用前要先将jar包fastjson-1.2.75.jar放入jmeter路径的lib文件夹下,文章中无法添加附件大家只能自行在网上搜索一下哪里可以下载这个jar包了

代码如下:
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONPath;
//import com.alibaba.fastjson.serializer.SerializerFeature;
//获取接口响应
String rspStr = prev.getResponseDataAsString();
JSONObject response = JSON.parseObject(rspStr);
//获取json对象中指定key的value
JSONObject data = response.get("data");
//判断条件后更新指定files的值
if (data.get("files") == null ){
log.info("值为Null,需要更新files");
JSONPath.set(data,"$.files","82ba9410-435c-11eb-a70a-497b0ce58495");
}
if (data.get("files") != null ){
log.info("值不为Null");
log.info(data.get("files"));
}
if (data.get("phone") == null ){
log.info("值为Null,需要更新phone");
JSONPath.set(data,"$.phone","13113111131");
}
//json对象转字符串,这一步toJSONString方法会自动排除掉值为null的key,目前不知道咋处理
String dataStr = JSON.toJSONString(data);
log.info(dataStr);
//字符串保存到jmeter变量
vars.put("rspdataStr",dataStr);
网友评论