JSON动态取值

package util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import entity.JsonDataVO;
import org.springframework.beans.BeanUtils;

import java.io.IOException;
import java.util.*;

public class JsonDataUtil {

    private void JsonToList(Stack<JSONObject> stObj, List<JsonDataVO> resultList, Integer index) throws IOException {

        if (index == null)
            index = 1;
        else
            index++;

        if (stObj == null && stObj.pop() == null) {
            return;
        }
        JSONObject json = stObj.pop();
        Set<String> keySet = json.keySet();
        for (String key : keySet) {
            Object value = json.get(key);
            JsonDataVO entity = new JsonDataVO();
            entity.setCurrentFieldVal(key);
            entity.setIndex(index);
            entity.setParentData(json);
            entity.setCurrentData(value);
            resultList.add(entity);
            if (value instanceof JSONObject) {
                stObj.push((JSONObject) value);
                // 递归遍历
                JsonToList(stObj, resultList, index);
            } else if (value instanceof JSONArray) {
                for (Object err : (JSONArray) value) {
                    if (err instanceof JSONObject) {
                        JSONObject errItem = (JSONObject) err;
                        stObj.push(errItem);
                        // 递归遍历
                        JsonToList(stObj, resultList, index);
                    }
                }
            }

        }
    }

    public List<JsonDataVO> getAllJsonValue(String jsonStr) {
        try {
            List<JsonDataVO> resultList = new ArrayList<JsonDataVO>();
            JSONObject obj = JSON.parseObject(jsonStr);
            Stack<JSONObject> stObj = new Stack<JSONObject>();
            stObj.push(obj);
            this.JsonToList(stObj, resultList, null);
            return resultList;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<JsonDataVO> getJsonData(List<JsonDataVO> list, Integer index, String fieldName) {
        List<JsonDataVO> tempList = new ArrayList<JsonDataVO>();
        for (JsonDataVO jsonDataVO : list) {
            if (jsonDataVO.getCurrentFieldVal().equals(fieldName)) {
                if (index != null) {
                    if (index.equals(jsonDataVO.getIndex())) {
                        tempList.add(jsonDataVO);
                    }
                } else {
                    tempList.add(jsonDataVO);
                }
            }
        }
        return tempList;
    }


    /**
     * 基于当前对象,计算level个等级的数据是什么
     *
     * @param list
     * @param jdVo
     * @param parentIndex (从1开始的)
     * @param fieldName
     * @return
     */
    public void getJsonDataByChild(List<JsonDataVO> list, JsonDataVO jdVo, int parentIndex, String fieldName) {
        //3, 1  3-1=2
        int level = jdVo.getIndex() - parentIndex;
        JsonDataVO temp = new JsonDataVO();
        BeanUtils.copyProperties(jdVo, temp);
        for (int i = 1; i <= level; i++) {
            int index = jdVo.getIndex() - i;
            List<JsonDataVO> tempList = new ArrayList<JsonDataVO>();
            list.forEach(a -> {
                if (a.getIndex() == index) {
                    tempList.add(a);
                }
            });
            if (tempList != null && tempList.size() > 0) {
                for (JsonDataVO jsonDataVO : tempList) {
                    if (jsonDataVO.getCurrentData() instanceof JSONArray) {
                        JSONArray jsonArray = (JSONArray) jsonDataVO.getCurrentData();
                        for (Object jObj : jsonArray) {
                            if (JSON.toJSONString(jObj).equals(JSON.toJSONString(temp.getParentData()))) {
                                temp = new JsonDataVO();
                                BeanUtils.copyProperties(jsonDataVO, temp);
                                break;
                            }
                        }
                    } else {
                        if (JSON.toJSONString(jsonDataVO.getCurrentData()).equals(JSON.toJSONString(temp.getParentData()))) {
                            temp = new JsonDataVO();
                            BeanUtils.copyProperties(jsonDataVO, temp);
                            break;
                        }
                    }
                }
            }
        }
    }


}

对象:

package entity;

import com.alibaba.fastjson.JSONObject;

public class JsonDataVO {

    private int index;
    private JSONObject parentData;
    private Object currentData;
    private String currentFieldVal;

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public JSONObject getParentData() {
        return parentData;
    }

    public void setParentData(JSONObject parentData) {
        this.parentData = parentData;
    }

    public Object getCurrentData() {
        return currentData;
    }

    public void setCurrentData(Object currentData) {
        this.currentData = currentData;
    }

    public String getCurrentFieldVal() {
        return currentFieldVal;
    }

    public void setCurrentFieldVal(String currentFieldVal) {
        this.currentFieldVal = currentFieldVal;
    }
}

 


版权声明:本文为y526089989原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。