JSON 多层一层化降维

2020-05-14  本文已影响0人  鬼魅魑魅

JSON 多层一层化降维

结果如下,具体在调整,因为数组key有重复,需要加编号

//输入
{
    "payloadID": {
        "abc": "88898"
    },
    "payloadName": "QVFeederLinkPayload",
    "payloadState": 4,
    "payloadBackupState": 5,
    "subbandNum": 3,
    "subbandInfo": [
        {
            "subbandID": 1,
            "subbandType": "type1",
            "subbandFreq": 4,
            "subbandBW": 5,
            "subbandWithBeam": 6
        },
        {
            "subbandID": 2,
            "subbandType": "type2",
            "subbandFreq": 4,
            "subbandBW": 5,
            "subbandWithBeam": 5
        },
        {
            "subbandID": 3,
            "subbandType": "type3",
            "subbandFreq": 4,
            "subbandBW": 5,
            "subbandWithBeam": 5
        }
    ]
}

// 输出
json_payloadID_abc:88898
json_payloadName:QVFeederLinkPayload
json_payloadState:4
json_payloadBackupState:5
json_subbandInfo0_subbandType:type1
json_subbandInfo0_subbandWithBeam:6
json_subbandInfo0_subbandBW:5
json_subbandInfo0_subbandFreq:4
json_subbandInfo0_subbandID:1
json_subbandInfo1_subbandType:type2
json_subbandInfo1_subbandWithBeam:5
json_subbandInfo1_subbandBW:5
json_subbandInfo1_subbandFreq:4
json_subbandInfo1_subbandID:2
json_subbandInfo2_subbandType:type3
json_subbandInfo2_subbandWithBeam:5
json_subbandInfo2_subbandBW:5
json_subbandInfo2_subbandFreq:4
json_subbandInfo2_subbandID:3
json_subbandNum:3

java


    /**
     * JSON 多层降低纬度处理
     * @param prefix 前缀
     * @param json
     * @param tempList 空list即可
     * @return
     */
    public static List<Map<String,Object>> parseJson(String prefix, JSONObject json, List<Map<String, Object>> tempList){
        Iterator<String> iterator = json.keySet().iterator();
        while(iterator.hasNext()){
            String key = iterator.next();
            String value = json.getString(key);
            JSONObject child = null;
            try {
                Object o = JSON.parse(value);
                if(o instanceof JSONArray) {
                    JSONArray jsonArray = JSONObject.parseArray(value);
                    for (int i = 0; i < jsonArray.size(); i++) {
                        child =(JSONObject) jsonArray.get(i);
                        parseJson(prefix + "_" + key + i, child, tempList);
                    }
                } else if (o instanceof JSONObject) {
                    child =(JSONObject) o;
                    parseJson(prefix + "_" + key, child, tempList);
                } else {
                    // temp do nothing
                    Map<String, Object> map = new HashMap<>(1);
                    map.put(prefix + "_" + key, value);
                    tempList.add(map);

                }
            } catch (Exception e) {
                System.err.println("key:" + key + ",value:" + value);
                Map<String, Object> map = new HashMap<>(1);
                map.put(prefix + "_" + key, value);
                tempList.add(map);
            }
        }
        return tempList;
    }

TEST


        String json = "{\"payloadID\":{\"abc\":\"88898\"},\"payloadName\":\"QVFeederLinkPayload\",\"payloadState\":4,\"payloadBackupState\":5,\"subbandNum\":3,\"subbandInfo\":[{\"subbandID\":1,\"subbandType\":\"type1\",\"subbandFreq\":4,\"subbandBW\":5,\"subbandWithBeam\":6},{\"subbandID\":2,\"subbandType\":\"type2\",\"subbandFreq\":4,\"subbandBW\":5,\"subbandWithBeam\":5},{\"subbandID\":3,\"subbandType\":\"type3\",\"subbandFreq\":4,\"subbandBW\":5,\"subbandWithBeam\":5}]}";
        JSONObject jsonObject = JSON.parseObject(json);
        List<Map<String,Object>> list = new ArrayList<>();
        List<Map<String,Object>> result = OIDUtil.parseJson("json", jsonObject, list);
        for (Map<String, Object> map : result) {
            Iterator<Map.Entry<String, Object>> a= map.entrySet().iterator();
            while (a.hasNext()) {
                Map.Entry<String, Object> bb = a.next();
                System.out.println(bb.getKey() + ":" + bb.getValue());
            }
        }
上一篇下一篇

猜你喜欢

热点阅读