一、準備接口數據(對比字段,即json數據中須要提取的key對應的值進行對比)java
二、配置獲取EXCEL數據shell
三、新建線程,並建兩個http請求,分別用於請求新舊接口json
四、提取須要對比的內容url
五、賦值變量,用於對比請求時取值進行對比spa
六、新建beanshell取樣器線程
七、新建beanshell斷言3d
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包code
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); } } }