讀取JSON並進行對比
阿新 • • 發佈:2018-12-21
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; } }