JsonPathUtils.java 3.63 KB
package com.lhcredit.common.utils;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import com.lhcredit.project.business.reportMake.ApiJsonPathService;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

public class JsonPathUtils {
    /**
     * [0].xxx
     * [0]
     * [0].xxx.aaa
     * @param path
     * @return
     */
    protected static Object path(Object object, String path){
        Object eval = null;
        try{
            if (path.substring(0,1).contains("[")){
                JSONArray arr= (JSONArray) object;
                eval=JSONPath.eval(arr, "$" + path.replace("$",""));
            }else{
                eval = JSONPath.eval(object, "$." + path.replace("$.",""));
            }
            return null!=eval?eval.toString():null;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static String jsonPathString(JSONObject object, String path) {
        try{
            Object path1 = path(object, path);
            return null!=path1?path1.toString():null;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    public static JSONObject getJSONObject(JSONObject object, String path) {
        try{
            Object path1 = path(object, path);
            return JSONObject.parseObject(path1.toString());
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    public static JSONArray getJSONArray(JSONObject object, String path) {
        try{
            Object path1 = path(object, path);
            return JSONArray.parseArray(path1.toString());
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    public static BigDecimal jsonPathBigDecimal(JSONObject object, String path) {
        try{
            Object path1 = path(object, path);
            return null!=path1?new BigDecimal(path1.toString().replace("万","")) :null;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    public static Date jsonPathDate(JSONObject object, String path) {
        try{
            Object path1 = path(object, path);
            return DateUtils.parseDate(path1.toString());
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    public static Integer jsonPathInteger(JSONObject object, String path) {
        try{
            Object path1 = path(object, path);
            Integer integer = Integer.valueOf(path1.toString());
            return integer;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    public static Double jsonPathDouble(JSONObject object, String path) {
        try{
            Object path1 = path(object, path);
            return Double.valueOf(path1.toString());
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    public static <T> T jsonPathValue(JSONObject object,String path,Class<T> tClass){
        try{
            Object path1 = path(object, path);
            return (T) path1;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


    public static JSONObject paseJSONObj(Object object) {
        if (object instanceof String) {
            return JSON.parseObject(object.toString());
        }else {
            return JSONObject.parseObject(JSON.toJSONString(object));
        }

    }
}