1、准备接口数据(对比字段,即json数据中需要提取的key对应的值进行对比)
2、配置获取EXCEL数据
3、新建线程,并建两个http请求,分别用于请求新旧接口
4、提取需要对比的内容
5、赋值变量,用于对比请求时取值进行对比
6、新建beanshell取样器
7、新建beanshell断言
import mytest.java.json.*; import net.sf.json.JSONObject; String resultV1 = vars.get("oldResult"); String resultV2 = vars.get("newResult"); //log.info(resultV1); JSONObject jsonObject1=JSONObject.fromObject(resultV1); JSONObject jsonObject2=JSONObject.fromObject(resultV2); //print(jsonObject1); JsonDiff jd = new JsonDiff(); //print(jsonObject1); String result = jd.compareJsonD(jsonObject1,jsonObject2,null); if(result != null){ Failure = true; FailureMessage=result; }
准备JAR包
package mytest.java.json; import java.util.Iterator; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class JsonDiff { public static void main(String[] args) { String a = "{\"milestone\": [],\"meeting\": [{\"room_address\": \"11F\",\"start\": \"2019-05-10 00:01:00\",\"meeting_type\": \"2\",\"end\": \"2019-05-11 23:30:00\",\"type\": \"1\",\"locale\": \"\u9C81\u73ED\",\"title\": \"JYF-TEST\u6B63\u5E38\u666E\u901A\u4F1A\u8BAE\",\"meeting_compere\": \"\u53F6\u654F\",\"url\":\"\\/v0\\/WOS\\/#\\/home\\/WOS\\/boardroom\\/personal?title=JYF-TEST\u6B63\u5E38\u666E\u901A\u4F1A\u8BAE&id=45838\"},{\"room_address\": \"10F\",\"start\": \"2019-05-10 00:04:00\",\"meeting_type\":\"3\",\"end\":\"2019-05-1023:30:00\",\"type\":\"1\",\"locale\":\"\u9C81\u73ED1\",\"title\":\"JYF-TEST\u6B63\u5E38\u4F8B\u4F1A\",\"meeting_compere\":\"\u53F6\u654F\",\"url\":\"\\/v0\\/WOS\\/#\\/home\\/WOS\\/boardroom\\/personal?title=JYF-TEST\u6B63\u5E38\u4F8B\u4F1A&id=45840\"}]}"; String b = "{\"milestone\": [],\"meeting\": [{\"room_address\": \"10F\",\"start\": \"2019-05-10 00:01:00\",\"meeting_type\": \"2\",\"end\": \"2019-05-11 23:30:00\",\"type\": \"1\",\"locale\": \"\u9C81\u73ED\",\"title\": \"JYF-TEST\u6B63\u5E38\u666E\u901A\u4F1A\u8BAE\",\"meeting_compere\": \"\u53F6\u654F\",\"url\":\"\\/v0\\/WOS\\/#\\/home\\/WOS\\/boardroom\\/personal?title=JYF-TEST\u6B63\u5E38\u666E\u901A\u4F1A\u8BAE&id=45838\"},{\"room_address\": \"10F\",\"start\": \"2019-05-10 00:04:00\",\"meeting_type\":\"3\",\"end\":\"2019-05-1023:30:00\",\"type\":\"1\",\"locale\":\"\u9C81\u73ED1\",\"title\":\"JYF-TEST\u6B63\u5E38\u4F8B\u4F1A\",\"meeting_compere\":\"\u53F6\u654F\",\"url\":\"\\/v0\\/WOS\\/#\\/home\\/WOS\\/boardroom\\/personal?title=JYF-TEST\u6B63\u5E38\u4F8B\u4F1A&id=45840\"}]}"; JSONObject json1 = JSONObject.fromObject(a); JSONObject json2 = JSONObject.fromObject(b); String t = compareJsonD(json1, json2,null); // System.out.print("****"); System.out.print(tmp); /* for (int i = 0; i < 10000; i++) { if (! json1.toString().equals( json1.toString())) { System.out.println(654565); } }*/ } static String tmp = null; public static String compareJsonD(JSONObject json1, JSONObject json2,String key) { tmp = null; compareJson(json1, json2,null); return tmp; } public static String compareJson(JSONObject json1, JSONObject json2,String key) { JSONObject jsonObject1=JSONObject.fromObject(json1); JSONObject jsonObject2=JSONObject.fromObject(json2); Iterator<String> i = jsonObject1.keys(); String result1 = ""; // String tmp = ""; while (i.hasNext()) { key = i.next(); result1 = compareJson(jsonObject1.get(key), jsonObject2.get(key),key); // System.out.println(result1); if(result1 != null){ if(tmp != null){ tmp += result1; } else{ tmp = result1; } } } // System.out.println("**********"); // System.out.println(tmp); return tmp; } public static String compareJson(Object json1,Object json2,String key) { String result2 = null; if ( json1 instanceof JSONObject ) { result2 = compareJson((JSONObject) json1 ,(JSONObject) json2,key); }else if ( json1 instanceof JSONArray ) { compareJson((JSONArray) json1 ,(JSONArray) json2,key); }else if(json1 instanceof String ){ result2 = compareJson((String) json1 ,(String) json2,key); }else { result2 = compareJson(json1.toString(), json2.toString(), key); } return result2; } public static String compareJson(String str1,String str2,String key) { String result = null; if (!str1.equals(str2)) { if (result != null) { result = "\r\n"+"key:"+key+"\r\n"+"json1:"+ str1 +"\r\n"+"json2:"+str2+"\r\n\r\n"; } else { result = "key:"+key+"\r\n"+"json1:"+ str1 +"\r\n"+"json2:"+str2+"\r\n\r\n"; } } return result; } public static void compareJson(JSONArray json1,JSONArray json2,String key) { Iterator i1= json1.iterator(); Iterator i2= json2.iterator(); while ( i1.hasNext()) { compareJson(i1.next(), i2.next(),key); } } }