读取JSON并进行对比

package util;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.util.*;
import java.util.regex.*;
 
public class Test1 {
    private static String filePath = System.getProperty("user.dir") +"/20181016/";
    private static Set<String> toCheck = new HashSet<>();
    private static Set<String> toBeCheck = new HashSet<>();
    public static void main(String[] args) throws IOException {
        validate("UserToAdress","name","$.data","check","$.data");
    }
 
    /**
     * 终极规整方法
     * @param srcFilePattern 源文件
     * @param srcExpression 源文件json要查找的节点
     * @param disFilePattern 目标文件
     * @param dstExpression 目标文件json要查找的节点
     * @throws IOException
     */
    private static void validate(String caseId,String srcFilePattern, String srcExpression, String disFilePattern, String dstExpression) throws IOException {
        toCheck=retrieveList(srcFilePattern,srcExpression);
        toBeCheck=retrieveList(disFilePattern,dstExpression);
        System.out.println(toCheck);
        System.out.println(toBeCheck);
        toCheck.removeAll(toBeCheck);
 
        System.out.println("These are "+toCheck.size()+" missing "+" "+ caseId);
        System.out.println("There are " +String.join(",",toCheck));
    }
 
    /**
     *
     * @param srcFileName 文件名
     * @param srcFilePattern 查找的json 节点
     * @return 结果集
     * @throws IOException
     */
    private static Set<String> retrieveList(String srcFileName, String srcFilePattern) throws IOException {
        ArrayList<String> names=readFileName(srcFileName);
        Set<String> target=new HashSet<>();
        for (String fileName:names){
            for (String result:validate(fileName,srcFilePattern)){
                //System.out.println(result);
                target.add(result);
            }
        }
        return target;
    }
 
    /**
     * 输入文件名,返回满足的所有文件名,例:输入name,返回存在的name,name1,name2,name3
     * @param fileName
     * @return 文件名list
     */
    public static ArrayList<String> readFileName(String fileName) {
            /**
             * 将目标目录封装成 File 对象。
             */
            File dir = new File(filePath);
            Pattern pattern =Pattern.compile(fileName +"[\\d]*.json");
 
            /**
             * 获取目录下的所有文件和文件夹
             */
            String[] names = dir.list();
            ArrayList<String> newName= new ArrayList<>();
            for (String name : names) {
                if (pattern.matcher(name).matches()){
                    //System.out.println(name);
                    newName.add(name);
                }
 
            }
            return newName;
        }
 
    /**
     *
     * @param srcFileName 源文件名字
     * @param srcFilePattern 需要查找的json节点 如:$..id查找二级节点id的值
     * @throws IOException
     * @return Set 返回结果集合
     */
    private static Set<String> validate(String srcFileName, String srcFilePattern) throws IOException {
        String json = new String(Files.readAllBytes(Paths.get(filePath+srcFileName)));
        Object document = Configuration.defaultConfiguration().jsonProvider().parse(json);
        Set<String> target=new HashSet<>();
        Set<String> targetNew=new HashSet<>();
        boolean flag="class net.minidev.json.JSONArray".equals(JsonPath.read(document,srcFilePattern).getClass().toString());
        System.out.println(JsonPath.read(document,srcFilePattern).getClass().toString());
        if (flag){
            List<Object> names = JsonPath.read(document,srcFilePattern);
            //System.out.println(names);
            Iterator<Object> iter = names.iterator();
            while(iter.hasNext()){  //执行过程中会执行数据锁定,性能稍差,若在循环过程中要去掉某个元素只能调用iter.remove()方法。
                target.add(iter.next()+"");
            }
        }else{
            String names = JsonPath.read(document,srcFilePattern)+"";
            target.add(names);
        }
        //获取出来的Set有重复,将重复的去重
        List<String> tempList= new ArrayList<>();
        for(String i:target){
            if(!tempList.contains(i)){
                tempList.add(i);
            }
        }
        for(String i:tempList){
            targetNew.add(i);
        }
        return targetNew;
    }
}

 

posted @ 2019-09-25 11:12  测试的世界很精彩  阅读(2113)  评论(2编辑  收藏  举报