jmeter - JMeter - JSON 响应操作在带有 java 的 JSR223 处理器中不起作用
问题描述
import net.minidev.json.parser.JSONParser;
import net.minidev.json.JSONObject;
import net.minidev.json.JSONArray;
JSONParser p = new JSONParser(JSONParser.MODE_PERMISSIVE);
String response = prev.getResponseDataAsString();
JSONObject jsonresponse = (JSONObject) p.parse(response);
JSONObject json2 = (JSONObject) jsonresponse.get("Key1");
JSONObject newjson = new JSONObject();
newjson.put("displayValue", json2.get("displayValue"));
newjson.put("value", json2.get("value"));
jsonresponse.put("Key2", newjson);
if(jsonresponse.has("Key3"))
{
jsonresponse.remove("Key3");
jsonresponse.put("Key3", jsonresponse.get("Key3").get("value");
}
log.info(jsonresponse.toString());
我需要进入 if 循环以删除 json 键值(如果存在)并将其替换为其他内容。
解决方案
- 我相信您至少需要将
if(jsonresponse.has("Key3"))
线路更改为if(jsonresponse.containsKey("Key3"))
- 根据 JMeter 最佳实践,您不应该使用 Beanshell 进行脚本编写,因此请考虑切换到 Groovy 语言,Groovy 的性能要好得多,而且它具有内置的 JSON 解析/生成功能。请参阅Apache Groovy - Why and How You Should Use It文章以获取全面的解释、基准测试、示例代码片段等。
推荐阅读
- java - 根据日期时间和持续时间列从表 t1 中选择学生待在一起的行
- python - 如何在匹配字符串的嵌套列表中找到索引的最小值和最大值?
- python - 安装tensorflow时如何修复虚拟环境中发生的EnvironmentError
- javascript - 在单个弹出窗口中显示具有相同纬度/经度坐标的多个标记
- python - 如何在 Python 中将元组列表读取为列表类型并将元组读取为以字符串形式编码的元组类型?
- html - 使用 CSS 在表格列中创建类似标记的效果
- javascript - 返回最大值(ASCII)
- python - 我正在尝试使用两个不同的异步定义(@client.event 和 @bot.command())但是其中一个不起作用
- javascript - 计算 2 lat/lons 之间道路折线上的距离
- c++ - 运行 IF 语句而不是 ELSE 语句需要什么?