其他分享
首页 > 其他分享> > json数据的key的读取和替换

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 = "{\n" +
                "\t\"ret\": \"0\",\n" +
                "\t\"retMsg\": null,\n" +
                "\t\"count\": 1,\n" +
                "\t\"processCost\": null,\n" +
                "\t\"data\": [{\n" +
                "\t\t\t\"moniid\": \"11\",\n" +
                "\t\t\t\"serialnumber\": \"12\",\n" +
                "\t\t\t\"monitype\": \"13\"\n" +
                "\t\t},\n" +
                "\t\t{\n" +
                "\t\t\t\"moniid\": \"22\",\n" +
                "\t\t\t\"serialnumber\": \"22\",\n" +
                "\t\t\t\"monitype\": \"23\"\n" +
                "\t\t},\n" +
                "\t\t{\n" +
                "\t\t\t\"moniid\": \"33\",\n" +
                "\t\t\t\"serialnumber\": \"32\",\n" +
                "\t\t\t\"monitype\": \"33\"\n" +
                "\t\t}\n" +
                "\t]\n" +
                "}";

        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());
    }

  

标签:jsonObj,读取,get,JSONArray,JSONObject,json,keyMap,key
来源: https://www.cnblogs.com/weihuang6620/p/11308832.html