json数据的key的读取和替换


读取json的key:

/**
     * @Description: 递归读取所有的key
     * @Param:
     * @return:
     * @throws Exception
     * @author: hw
     * @date: 2019/7/31 15:18
     */
    public static Set<String> getAllKey(JSONObject jsonObject) {
        Set<String> myset = new HashSet<>();
        Iterator<String> keys = jsonObject.keySet().iterator();// jsonObject.keys();
        while (keys.hasNext()) {
            String key = keys.next();
            myset.add(key.toString());
            if (jsonObject.get(key) instanceof JSONObject) {
                JSONObject innerObject = (JSONObject) jsonObject.get(key);
                myset.addAll(getAllKey(innerObject));
            } else if (jsonObject.get(key) instanceof JSONArray) {
                JSONArray innerObject = (JSONArray) jsonObject.get(key);
                myset.addAll(getAllKey(innerObject));
            }
        }
        return myset;
    }

    public static Set<String> getAllKey(JSONArray json1) {
        Set<String> myset = new HashSet<>();
        if (json1 != null ) {
            Iterator i1 = json1.iterator();
            while (i1.hasNext()) {
                Object key = i1.next();
                if (key instanceof  JSONObject) {
                    JSONObject innerObject = (JSONObject) key;
                    myset.addAll(getAllKey(innerObject));
                } else if (key instanceof JSONArray) {
                    JSONArray innerObject = (JSONArray) key;
                    myset.addAll(getAllKey(innerObject));
                }
            }
        }
        return myset;
    }

  

替换json的key:

/**
    * @Description: 替换json数据的key
    * @Param:
    * @return:
    * @throws Exception
    * @author: hw
    * @date: 2019/7/31 16:22
    */
    public static JSONObject changeJsonObj(JSONObject jsonObj,Map<String, String> keyMap) {
        JSONObject resJson = new JSONObject();
        Set<String> keySet = jsonObj.keySet();
        for (String key : keySet) {
            String resKey = keyMap.get(key) == null ? key : keyMap.get(key);
            try {
                if (jsonObj.get(key) instanceof JSONObject) {
                    JSONObject jsonobj1 = (JSONObject) jsonObj.get(key);
                    resJson.put(resKey, changeJsonObj(jsonobj1, keyMap));
                } else if (jsonObj.get(key) instanceof JSONArray) {
                    JSONArray jsonArr = (JSONArray) jsonObj.get(key);
                    resJson.put(resKey, changeJsonArr(jsonArr, keyMap));
                }else {
                    resJson.put(resKey, jsonObj.get(key));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return resJson;
    }

    public static JSONArray changeJsonArr(JSONArray jsonArr,Map<String, String> keyMap) {
        JSONArray resJson = new JSONArray();
        for (int i = 0; i < jsonArr.size(); i++) {
            JSONObject jsonObj = jsonArr.getJSONObject(i);
            resJson.add(changeJsonObj(jsonObj, keyMap));
        }
        return resJson;
    }

  

测试方法:

public static void main(String[] args) {
        String test = "{
" +
                "	"ret": "0",
" +
                "	"retMsg": null,
" +
                "	"count": 1,
" +
                "	"processCost": null,
" +
                "	"data": [{
" +
                "			"moniid": "11",
" +
                "			"serialnumber": "12",
" +
                "			"monitype": "13"
" +
                "		},
" +
                "		{
" +
                "			"moniid": "22",
" +
                "			"serialnumber": "22",
" +
                "			"monitype": "23"
" +
                "		},
" +
                "		{
" +
                "			"moniid": "33",
" +
                "			"serialnumber": "32",
" +
                "			"monitype": "33"
" +
                "		}
" +
                "	]
" +
                "}";

        Map<String, String> keyMap = new HashMap<String, String>();
        keyMap.put("count", "param1");
        keyMap.put("processCost", "param2");
        keyMap.put("desc", "param3");
        keyMap.put("moniid", "param4");
        keyMap.put("serialnumber", "param5");
        keyMap.put("monitype", "param6");
        keyMap.put("data", "param7");
        JSONObject jsonObj = changeJsonObj(JSONObject.parseObject(test),keyMap);
        System.out.println(jsonObj.toString());
    }

  

原文地址:https://www.cnblogs.com/weihuang6620/p/11308832.html