Java json字串對比

來源:互聯網
上載者:User

標籤:ret   string   compare   public   nod   try   ref   dna   style   

public class JsonUtil {    public static boolean compareJsonText(String str1, String str2) {        return compareJsonNode(JsonUtil.readTree(str1), JsonUtil.readTree(str2));    }        public static boolean compareJsonNode(JsonNode node1, JsonNode node2) {        if(node1.isObject()) {            if(!node2.isObject()) return false;            if(!compareFieldNames(node1.fieldNames(), node2.fieldNames()))                return false;            Iterator<Entry<String,JsonNode>> fields1 = node2.fields();            Map<String,JsonNode> fields2 = getFields(node1);            boolean flag = true;            while(fields1.hasNext()){                Entry<String,JsonNode> field1 = fields1.next();                JsonNode field2 = fields2.get(field1.getKey());                if(!compareJsonNode(field1.getValue(), field2))                    flag = false;            }            return flag;        } else if(node1.isArray()) {            if(!node2.isArray()) return false;            return compareArrayNode(node1, node2);        } else {            return node1.toString().equals(node2.toString());        }    }        public static boolean compareArrayNode(JsonNode node1, JsonNode node2){        Iterator<JsonNode> it1 = node1.elements();        while(it1.hasNext()){            boolean flag = false;            JsonNode node = it1.next();            Iterator<JsonNode> it2 = node2.elements();            while(it2.hasNext()){                if(compareJsonNode(node, it2.next())){                    flag = true;                    break;                }            }            if(!flag)                return false;        }        return true;    }        public static boolean compareFieldNames(Iterator<String> it1, Iterator<String> it2) {        List<String> nameList1 = new ArrayList<String>();        List<String> nameList2 = new ArrayList<String>();        while(it1.hasNext()){            nameList1.add(it1.next());        }        while(it2.hasNext()){            nameList2.add(it2.next());        }        return nameList1.containsAll(nameList2) && nameList2.containsAll(nameList1);    }        public static Map<String, JsonNode> getFields(JsonNode node) {        Iterator<Entry<String,JsonNode>> fields = node.fields();        Map<String, JsonNode> fieldMap = new HashMap<String, JsonNode>();        while(fields.hasNext()){            Entry<String,JsonNode> field = fields.next();            fieldMap.put(field.getKey(), field.getValue());        }        return fieldMap;    }}

 

Java json字串對比

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.