JSONUtils


package com.yh.proxy.pay.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.Map;

/**
 * @author aliqingge
 * @date 2020/6/15 16:04
 */
public class JsonUtils {

    private static boolean isReplace = false;

    /**
     * 获取指定json属性对应得数据
     *
     * @param objJson
     * @param reg
     * @return
     */
    public static Object getValueByKey(Object objJson, String reg) {
        //如果obj为json数组
        if (objJson instanceof JSONArray) {
            JSONArray objArray = (JSONArray) objJson;
            for (int i = 0; i < objArray.size(); i++) {
                return getValueByKey(objArray.get(i), reg);
            }
        }
        //如果为json对象
        else if (objJson instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) objJson;
            for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                String key = entry.getKey();
                if (reg.equals(key)) {
                    return entry.getValue();
                }
                Object o = find(reg, jsonObject, key);
                if (null != o) {
                    return o;
                }
            }
        }
        return null;
    }

    /**
     * 获取指定json属性对应得数据
     *
     * @param objJson
     * @param reg
     * @return
     */
    public static boolean isValueByKey(Object objJson, String reg) {
        //如果obj为json数组
        if (objJson instanceof JSONArray) {
            JSONArray objArray = (JSONArray) objJson;
            for (int i = 0; i < objArray.size(); i++) {
                getValueByKey(objArray.get(i), reg);
            }
        }
        //如果为json对象
        else if (objJson instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) objJson;
            for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                String key = entry.getKey();
                if (reg.equals(key)) {
                    return true;
                }
                find(reg, jsonObject, key);
            }
        }
        return false;
    }

    /**
     * 根据key 替换原位置数据
     *
     * @param oldJson
     * @param newJson
     * @param reg
     * @return
     */
    public static boolean replaceValueByKey(Object oldJson, Object newJson, String reg) {
        //如果obj为json数组
        if (oldJson instanceof JSONArray) {
            JSONArray objArray = (JSONArray) oldJson;
            for (int i = 0; i < objArray.size(); i++) {
                getValueByKey(objArray.get(i), reg);
            }
        }
        //如果为json对象
        else if (oldJson instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) oldJson;
            for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                String key = entry.getKey();
                if (reg.equals(key)) {
                    entry.setValue(newJson);
                    JsonUtils.isReplace = true;
                }
                Object object = jsonObject.get(key);
                //如果得到的是数组
                if (object instanceof JSONArray) {
                    JSONArray objArray = (JSONArray) object;
                    replaceValueByKey(objArray, newJson, reg);
                }
                //如果key中是一个json对象
                else if (object instanceof JSONObject) {
                    replaceValueByKey(object, newJson, reg);
                }
            }
        }
        return JsonUtils.isReplace;
    }

    private static Object find(String reg, JSONObject jsonObject, String key) {
        Object object = jsonObject.get(key);
        //如果得到的是数组
        if (object instanceof JSONArray) {
            JSONArray objArray = (JSONArray) object;
            return getValueByKey(objArray, reg);
        }
        //如果key中是一个json对象
        else if (object instanceof JSONObject) {
            return getValueByKey(object, reg);
        }
        return null;
    }
}




  public static void main(String[] args) throws IOException {

        String apiUrl = "https://api.trongrid.io/event/transaction/54fd034c2c6b35a9810f815becf84ca2e7b0572b959d7ff6a05db92b32c7570c";
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        Request request = new Request.Builder()
                .url(apiUrl)
                .method("GET", null)
                .build();
        Response response = client.newCall(request).execute();
        if (response.code() == 200) {
            ResponseBody body = response.body();
            String data = body.string();
            Object succesResponse = JSON.parse(data);
            Object value = JsonUtils.getValueByKey(succesResponse, "result");
            Object value1 = JsonUtils.getValueByKey(value, "value");
            System.out.println(String.valueOf(value1));
        }
    }
上一篇 下一篇