All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tools.json.AutoComparation Maven / Gradle / Ivy

There is a newer version: 0.2.2
Show newest version
package tools.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tools.poi.ExcelDataSource;
import tools.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: zhengyu
 * @date: 2019/4/10
 * @protocol: N/A.
 * @apiName: AutoComparation.compareVariables
 * @description:
 */
public class AutoComparation {

    private static final Logger LOGGER = LoggerFactory.getLogger(AutoComparation.class);

    public static List compareVariables(String excelFilePath, String sheetName, String jsonFilePath){
        ExcelDataSource edc = new ExcelDataSource(excelFilePath,sheetName);
        int totalRow = edc.excelGetRows();
        System.out.println("Excel行数:"+totalRow);
        List actualList = AutoComparation.countVariableFromJson(jsonFilePath);
        List ignoreCaseActualList = actualList.stream()
                .map(String::toLowerCase)
                .collect(Collectors.toList());
        //之所以要将实际变量名转为小写,是因为有些变量预期名在Excel中写的不一定是驼峰形式
        System.out.println("小写变量列表:"+ignoreCaseActualList);

        List notFoundList = new ArrayList<>();
        for (int i = 0; i countVariableFromJson(String filePath){
        List variableList = new ArrayList<>();
        JSONObject jsonObject = JSON.parseObject(StringUtils.readFileAsString(filePath));
        int size = jsonObject.size();
        System.out.println("size = "+size);

        getValue(jsonObject,variableList);

        System.out.println("JSON串中一共有 "+variableList.size()+" 个去重复变量: ");
        System.out.println(variableList);
        return variableList;
    }

    public static void getValue(JSONObject jsonObject, List list){
        for (Map.Entry entry : jsonObject.entrySet()){
            if (entry.getValue() instanceof JSONObject) {
                System.out.print(entry.getKey() + " : ");
                System.out.println(jsonObject.getJSONObject(entry.getKey()));
                getValue(jsonObject.getJSONObject(entry.getKey()), list);
            }else if (entry.getValue() instanceof JSONArray && !"repayRecordsIn24Months".contentEquals(entry.getKey())){
                System.out.print(entry.getKey() + " : ");
                System.out.println("JSONArray: "+jsonObject.getJSONArray(entry.getKey()));
                JSONArray jsonArray = jsonObject.getJSONArray(entry.getKey());
                for (JSONObject jsonObjectTemp : jsonArray.toJavaList(JSONObject.class)){
                    getValue(jsonObjectTemp, list);
                }
            }else {
                if (!list.contains(entry.getKey())){
                    list.add(entry.getKey()); //存储JSON串中最后一层变量名的list。
                }
            }
        }
    }

    /* For M Project OpenApi Project */
    public static void setKeyValueToList(JSONObject jsonObject, List list){
        for (Map.Entry entry : jsonObject.entrySet()){
            if (entry.getValue() instanceof JSONObject) {
                System.out.print(entry.getKey() + " : ");
                System.out.println(jsonObject.getJSONObject(entry.getKey()));
                setKeyValueToList(jsonObject.getJSONObject(entry.getKey()), list);
            }else if (entry.getValue() instanceof JSONArray && !"repayRecordsIn24Months".contentEquals(entry.getKey())){
                System.out.print(entry.getKey() + " : ");
                System.out.println("JSONArray: "+jsonObject.getJSONArray(entry.getKey()));
                JSONArray jsonArray = jsonObject.getJSONArray(entry.getKey());
                for (JSONObject jsonObjectTemp : jsonArray.toJavaList(JSONObject.class)){
                    setKeyValueToList(jsonObjectTemp, list);
                }
            }else {
//                if (!list.contains(entry.getKey())){//去重复
                list.add(entry.getKey()+"="+entry.getValue()); //存储JSON串中最后一层变量名的list。
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy