zoukankan      html  css  js  c++  java
  • Java json字符串对比

    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;
        }
    }
  • 相关阅读:
    观察者模式
    php中compact,extract,list函数的使用
    加密解密
    python 线程池proxypool
    python qq音乐下载
    python 网易云音乐
    python 知识点
    python 格式化 header
    python 爬取百度图片
    php 过滤掉多维数组空值
  • 原文地址:https://www.cnblogs.com/anxiao/p/7810717.html
Copyright © 2011-2022 走看看