JMeter - JSON 响应操作在具有 java 的 JSR223 处理器中不起作用
JMeter - JSON response manipulation is not working in JSR223 processor with java
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 Best Practices you should not be using Beanshell for scripting so consider switching to Groovy language, Groovy performs much better and moreover it has built-in JSON parsing/generating capabilities. See Apache Groovy - Why and How You Should Use It 文章进行全面解释、基准测试、示例代码片段等
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 Best Practices you should not be using Beanshell for scripting so consider switching to Groovy language, Groovy performs much better and moreover it has built-in JSON parsing/generating capabilities. See Apache Groovy - Why and How You Should Use It 文章进行全面解释、基准测试、示例代码片段等