1. 需提前准备fastJSON包导进Jmeter的bin目录
2. 在for循环里判断value是否为JSONObject或者JSONAraay,是否继续递归,从而遍历所有键值对。
3. 获取到键值对后可以自己添加对键值对的处理方法。下面是对键值对进行简单的判断是否为空的情况。
// 导包
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;
//获取响应数据
String response = prev.getResponseDataAsString();
JSONObject jsonObject = JSONObject.parseObject(response);
//JSONObject遍历器
public static jsonInterator(JSONObject jo){
for(Map.Entry entry: jo.entrySet()){
String key = entry.getKey();
Object value = entry.getValue();
/*------断言value为空的情况 --------
* str.length() == 0 : 键值为空
* str.equals("{}") : JSON为空
* str.equals("[]") : JSONAraay为空
*/
String str = value.toString();
if(str.length() == 0 ){
Failure = true;
FailureMessage = key + " 值为空";
}
//嵌套JSON递归
if(value instanceof JSONObject){
jsonInterator(value);
}else if(value instanceof JSONArray){ //JSONAraay 递归
for(JSONObject json : value){
jsonInterator(json);
}
}else{
//System.out.println(key+"==="+value);
}
}
}
jsonInterator(jsonObject);