在開發中有時候會遇到兩個Json對象要比對內容是否一致的問題,好比以下兩個JSON,只是部分順序打亂java
{ "code": "19N3L08", "code1": "3110100252", "code2": "TX3110-014-160097", "code3": "3100", "code4": "3110", "code5": "LEHY-MRL-II", "msgList": [ { "message": "B32053A20", "message1": "TSXB3200", "message2": "B320" }, { "message": "B33053A20", "message1": "TSXB3300", "message2": "B330" } ] }
{ "code4": "3110", "code2": "TX3110-014-160097", "code1": "3110100252", "code": "19N3L08", "code3": "3100", "code5": "LEHY-MRL-II", "msgList": [ { "message": "B33053A20", "message1": "TSXB3300", "message2": "B330" }, { "message": "B33053A20", "message1": "TSXB3300", "message2": "B330" } ] }
1.使用反射獲取到Result.class的全部get方法,循環遍歷比對兩個對象,若是對象的內容相等則返回truejson
private boolean checkEleInfoEqual2(Result strJson, Result tarJson) { boolean result = true; try { for (Field field : Result.class.getDeclaredFields()) { String name = field.getName(); Method m = Result.class.getMethod("get" + upperCase1th(name)); if ((m.invoke(strJson) instanceof String) && !strEquals((String) m.invoke(strJson), (String) m.invoke(tarJson))) { result = false; } } result = result && listEquals(strJson.getMsgList(), tarJson.getMsgList()); } catch (Exception e) { result = false; e.printStackTrace(); } System.out.println(result); return result; }
2.重寫Result.class和Msg.class中的equals方法,若是對象的內容相等,則返回true數組
重寫了equals方法後,能夠直接將兩個Result對象使用equals方法進行判斷對象的內容相等,對於對象中的List結構則可使用List的contains方法判斷對象是否在List中,原理是在contains方法在ArrayList中經過indexOf比對數據,indexOf中遍歷數組使用重寫過的equals方法判斷。ide
public boolean contains(Object o) { return indexOf(o) >= 0; } public int indexOf(Object o) { if (o == null) { for (int i = 0; i < size; i++) if (elementData[i]==null) return i; } else { for (int i = 0; i < size; i++) if (o.equals(elementData[i])) return i; } return -1; }
package com.example.json; import com.alibaba.fastjson.JSONObject; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.List; /** * @Description: * @author: wei.wang * @since: 2019/11/13 15:36 * @history: 1.2019/11/13 created by wei.wang */ public class BeanEqual { private static String strJson = "{\n" + " \"code\": \"19N3L08\",\n" + " \"code1\": \"3110100252\",\n" + " \"code2\": \"TX3110-014-160097\",\n" + " \"code3\": \"3100\",\n" + " \"code4\": \"3110\",\n" + " \"code5\": \"LEHY-MRL-II\",\n" + " \"msgList\": [\n" + " {\n" + " \"message\": \"B32053A20\",\n" + " \"message1\": \"TSXB3200\",\n" + " \"message2\": \"B320\"\n" + " },\n" + " {\n" + " \"message\": \"B33053A20\",\n" + " \"message1\": \"TSXB3300\",\n" + " \"message2\": \"B330\"\n" + " },\n" + " {\n" + " \"message\": \"B37053A2019\",\n" + " \"message1\": \"TSXB37002\",\n" + " \"message2\": \"B370\"\n" + " },\n" + " {\n" + " \"message\": \"F31053A20\",\n" + " \"message1\": \"TSXF3100\",\n" + " \"message2\": \"F310\"\n" + " },\n" + " {\n" + " \"message\": \"F31053A20\",\n" + " \"message1\": \"TSXF3100\",\n" + " \"message2\": \"F380\"\n" + " },\n" + " {\n" + " \"message\": \"F31053A20\",\n" + " \"message1\": \"TSXF3100\",\n" + " \"message2\": \"F300\"\n" + " }\n" + " ]\n" + "}"; private static String tarJson = "{\n" + " \"code4\": \"3110\",\n" + " \"code2\": \"TX3110-014-160097\",\n" + " \"code1\": \"3110100252\",\n" + " \"code\": \"19N3L08\",\n" + " \"code3\": \"3100\",\n" + " \"code5\": \"LEHY-MRL-II\",\n" + " \"msgList\": [\n" + "\t {\n" + " \"message\": \"B33053A20\",\n" + " \"message1\": \"TSXB3300\",\n" + " \"message2\": \"B330\"\n" + " },\n" + " {\n" + " \"message\": \"B37053A2019\",\n" + " \"message1\": \"TSXB37002\",\n" + " \"message2\": \"B370\"\n" + " },\n" + " {\n" + " \"message\": \"F31053A20\",\n" + " \"message1\": \"TSXF3100\",\n" + " \"message2\": \"F300\"\n" + " },\n" + " {\n" + " \"message\": \"B32053A20\",\n" + " \"message1\": \"TSXB3200\",\n" + " \"message2\": \"B320\"\n" + " },\n" + " {\n" + " \"message\": \"F31053A20\",\n" + " \"message1\": \"TSXF3100\",\n" + " \"message2\": \"F310\"\n" + " },\n" + " {\n" + " \"message\": \"F31053A20\",\n" + " \"message1\": \"TSXF3100\",\n" + " \"message2\": \"F380\"\n" + " }\n" + " ]\n" + "}"; public static void main(String[] args) { //將獲取到的JSON轉爲Result對象 Result r1 = JSONObject.parseObject(strJson, Result.class); Result r2 = JSONObject.parseObject(tarJson, Result.class); System.out.println(strJson); System.out.println(tarJson); BeanEqual test = new BeanEqual(); test.checkEleInfoEqual2(r1, r2); } public static boolean listEquals(List<Msg> l1, List<Msg> l2) { boolean result = true; if (l1.size() != l2.size()) { result = false; System.out.println("行數據量不一致"); } for (Msg msg : l1) { if (!l2.contains(msg)) { result = false; System.out.println(msg.getMessage() + "數據不一致"); } else { l2.remove(msg); } } if (l2.size() > 0) { result = false; } return result; } /** * 重寫equals方法 * * @param strJson * @param tarJson */ private void checkEleInfoEqual(Result strJson, Result tarJson) { System.out.println(strJson.equals(tarJson)); } /** * 反射獲取值 * * @param strJson * @param tarJson */ private boolean checkEleInfoEqual2(Result strJson, Result tarJson) { boolean result = true; try { for (Field field : Result.class.getDeclaredFields()) { String name = field.getName(); Method m = Result.class.getMethod("get" + upperCase1th(name)); if ((m.invoke(strJson) instanceof String) && !strEquals((String) m.invoke(strJson), (String) m.invoke(tarJson))) { result = false; } } result = result && listEquals(strJson.getMsgList(), tarJson.getMsgList()); } catch (Exception e) { result = false; e.printStackTrace(); } System.out.println(result); return result; } /** * 首字符大寫 * * @param str * @return */ public static String upperCase1th(String str) { return str.substring(0, 1).toUpperCase() + str.substring(1); } /** * 字符串相等 * * @param str * @param tar * @return */ public static boolean strEquals(String str, String tar) { //都爲空,或者都不爲空且相等,數字判斷 if (isStrEmpty(str) && isStrEmpty(tar) || (!isStrEmpty(str) && !isStrEmpty(tar) && (str.equals(tar) || numberEqual(str, tar)))) { return true; } return false; } /** * 字符是否爲空 * * @param str * @return */ public static boolean isStrEmpty(String str) { return str == null || str.trim().length() == 0; } /** * 校驗數字相等 2.0 2.00 2 * * @param str * @param tar * @return */ public static boolean numberEqual(String str, String tar) { try { double strNum = Double.valueOf(str); double tarNum = Double.valueOf(tar); return strNum == tarNum; } catch (Exception e) { //若是拋出異常,返回False return false; } } }
package com.example.json; /** * @Description: * @author: wei.wang * @since: 2019/11/13 15:33 * @history: 1.2019/11/13 created by wei.wang */ public class Msg { private String message; private String message1; private String message2; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String getMessage1() { return message1; } public void setMessage1(String message1) { this.message1 = message1; } public String getMessage2() { return message2; } public void setMessage2(String message2) { this.message2 = message2; } @Override public boolean equals(Object obj) { return BeanEqual.strEquals(message, ((Msg) obj).getMessage()) && BeanEqual.strEquals(message1, ((Msg) obj).getMessage1()) && BeanEqual.strEquals(message2, ((Msg) obj).getMessage2()); } }
package com.example.json; import java.util.List; /** * @Description: * @author: wei.wang * @since: 2019/11/13 15:33 * @history: 1.2019/11/13 created by wei.wang */ public class Result { private String code; private String code1; private String code2; private String code3; private String code4; private String code5; private List<Msg> msgList; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getCode1() { return code1; } public void setCode1(String code1) { this.code1 = code1; } public String getCode2() { return code2; } public void setCode2(String code2) { this.code2 = code2; } public String getCode3() { return code3; } public void setCode3(String code3) { this.code3 = code3; } public String getCode4() { return code4; } public void setCode4(String code4) { this.code4 = code4; } public String getCode5() { return code5; } public void setCode5(String code5) { this.code5 = code5; } public List<Msg> getMsgList() { return msgList; } public void setMsgList(List<Msg> msgList) { this.msgList = msgList; } //重寫Result的equals方法,若是內容相等則返回true @Override public boolean equals(Object obj) { return BeanEqual.strEquals(code, ((Result) obj).getCode()) && BeanEqual.strEquals(code1, ((Result) obj).getCode1()) && BeanEqual.strEquals(code2, ((Result) obj).getCode2()) && BeanEqual.strEquals(code3, ((Result) obj).getCode3()) && BeanEqual.strEquals(code4, ((Result) obj).getCode4()) && BeanEqual.strEquals(code5, ((Result) obj).getCode5()) && BeanEqual.listEquals(msgList, ((Result) obj).getMsgList() ); } }