• 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;
        }
    }
  • 相关阅读:
    HTTP处理程序介绍
    c# Enum获取name,value和description
    如何成为优秀的软件人才
    关于系统设计分层
    从DLL中加载启动窗体
    摩斯密码
    休息下
    关于博文转载
    整合TextBox与Label 创建新控件EFLabelText
    ProC连接Oracle
  • 原文地址:https://www.cnblogs.com/anxiao/p/7810717.html
Copyright © 2020-2023  润新知