获取jsonPath,对比两个json是否相等

文章目录

  • mavne的pom文件
  • 实现代码
  • 执行结果

mavne的pom文件

    
        
        
            com.alibaba
            fastjson
            1.2.31
        

        
            org.testng
            testng
            6.11
        
    

实现代码

    /**
     * @param jsonObject
     * @return
     */
    public static  List getListJson(JSONObject jsonObject) {
        // 获取到所有jsonpath后,获取所有的key
        List jsonPaths = JSONPath.paths(jsonObject).keySet()
                .stream()
                .collect(Collectors.toList());
        //去掉根节点key
        List parentNode = new ArrayList<>();
        //根节点key
        parentNode.add("/");
        //循环获取父节点key,只保留叶子节点
        for (int i = 0; i < jsonPaths.size(); i++) {
            if (jsonPaths.get(i).lastIndexOf("/") > 0) {
                parentNode.add(jsonPaths.get(i).substring(0, jsonPaths.get(i).lastIndexOf("/")));
            }
        }

        //remove父节点key
        for (String parentNodeJsonPath : parentNode) {
            jsonPaths.remove(parentNodeJsonPath);
        }

        List jsonPathList = new ArrayList<>();
        Iterator jsonPath = jsonPaths.iterator();
        //将/替换为点.
        while (jsonPath.hasNext()) {
            jsonPathList.add(jsonPath.next().replaceFirst("/", "").replaceAll("/", "."));
        }
        //排序
        Collections.sort(jsonPathList);
        return jsonPathList;
    }

    /**
     * 获取两个Json相同的JsonPath
     * @param oneJson
     * @param twoJson
     * @return
     */
    public static  Set getTwoJsonSameJsonPath(JSONObject oneJson,JSONObject twoJson){
        List oneListJsonPath = getListJson(oneJson);
        List twoListJsonPath = getListJson(twoJson);
        //获取两个json有相同路径的jsonPath
        Set sameJsonPath = new HashSet();
        for (String oneJsonPath:oneListJsonPath){
            for (String twoJsonPath:twoListJsonPath){
                if (oneJsonPath.equals(twoJsonPath)){
                    sameJsonPath.add(oneJsonPath);
                    break;
                }
            }
        }
        return sameJsonPath;
    }


    /**
     * 获取两个json所有的jsonPath
     * @param oneJson
     * @param twoJson
     * @return
     */
    public static  Set getTwoJsonAllJsonPath(JSONObject oneJson,JSONObject twoJson){
        List oneListJsonPath = getListJson(oneJson);
        List twoListJsonPath = getListJson(twoJson);
        //获取两个json有相同路径的jsonPath
        Set allJsonPath = new HashSet();
        allJsonPath.addAll(oneListJsonPath);
        allJsonPath.addAll(twoListJsonPath);

        return allJsonPath;
    }


    /**
     * 通过jsonPath去获取两个json的value,并校验类型和值是否相等
     * @param jsonPathList
     * @param oneJson
     * @param twoJson
     */
    public static void checkTwoJsonSame(Set jsonPathList,JSONObject oneJson,JSONObject twoJson){
        for (Object jsonPath:jsonPathList){

            Object oneJsonValue = JSONPath.eval(oneJson,jsonPath.toString());
            Object twoJsonValue = JSONPath.eval(twoJson,jsonPath.toString());

            if (!JSONPath.contains(oneJson,jsonPath.toString())){
                System.out.println(String.format("oneJsonPath字段不存在,jsonPath = %s ,value2 = %s",jsonPath,twoJsonValue));
                break;
            }else if (!JSONPath.contains(twoJson,jsonPath.toString())){
                System.out.println(String.format("twoJsonPath字段不存在,jsonPath = %s ,value1 = %s",jsonPath,oneJsonValue));
                break;
            }

            if (oneJsonValue.equals(twoJsonValue)){
                System.out.println(String.format("相同,jsonPath = %s,value = %s",jsonPath,oneJsonValue));
            } else if (String.valueOf(oneJsonValue).equals(String.valueOf(twoJsonValue))){
                System.out.println(String.format("字段类型不相同,jsonPath = %s,value1 = %s,value2 = %s",jsonPath,oneJsonValue,twoJsonValue));
            } else {
                System.out.println(String.format("字段值不相同,jsonPath = %s,value1 = %s,value2 = %s",jsonPath,oneJsonValue,twoJsonValue));
            }
        }
    }


    @Test(description = "判断两个json是否相等")
    public void checkTwoJson(){
        JSONObject oneJsonObject = new JSONObject();
        JSONPath.set(oneJsonObject,"data.person","个人");
        JSONPath.set(oneJsonObject,"data.student.age","20");
        JSONPath.set(oneJsonObject,"data.student.name","张三");

        JSONObject twoJsonObject = new JSONObject();
        JSONPath.set(twoJsonObject,"data.person","个人");
        JSONPath.set(twoJsonObject,"data.student.age",20);

        Set allJsonPath = getTwoJsonAllJsonPath(oneJsonObject,twoJsonObject);
        checkTwoJsonSame(allJsonPath,oneJsonObject,twoJsonObject);
    }

    @Test(description = "判断两个json共同存在字段的字段值是否相等")
    public void checkTwoJsonSame(){
        JSONObject oneJsonObject = new JSONObject();
        JSONPath.set(oneJsonObject,"data.person","个人");
        JSONPath.set(oneJsonObject,"data.student.age","20");
        JSONPath.set(oneJsonObject,"data.student.name","张三");

        JSONObject twoJsonObject = new JSONObject();
        JSONPath.set(twoJsonObject,"data.person","个人");
        JSONPath.set(twoJsonObject,"data.student.age",20);

        Set sameJsonPath = getTwoJsonSameJsonPath(oneJsonObject,twoJsonObject);
        checkTwoJsonSame(sameJsonPath,oneJsonObject,twoJsonObject);
    }

执行结果

用例1:
相同,jsonPath = data.person,value = 个人
字段类型不相同,jsonPath = data.student.age,value1 = 20,value2 = 20
twoJsonPath字段不存在,jsonPath = data.student.name ,value1 = 张三
用例2:
相同,jsonPath = data.person,value = 个人
字段类型不相同,jsonPath = data.student.age,value1 = 20,value2 = 20

你可能感兴趣的:(java,java,jsonPath,json,maven,fastjson)