序列化与反序列化

1 KV结构且K不确定

Example:

public static void main(String[] args) {
        String response =
                "{\n" +
                        "  \"rule\":[\n" +
                        "        {\n" +
                        "            \"level\":\"info\",\n" +
                        "            \"on\": true,\n" +
                        "            \"ipList\":[],\n" +
                        "            \"router\":[\n" +
                        "              {\n" +
                        "                \"type\": \"default_type\",\n" +
                        "                \"id\":\"12312\"\n" +
                        "              }\n" +
                        "            ]\n" +
                        "        },\n" +
                        "        {\n" +
                        "            \"level\":\"error\",\n" +
                        "            \"on\": false,\n" +
                        "            \"ipList\":[\"127.0.0.1\"],\n" +
                        "            \"router\":[]\n" +
                        "        }\n" +
                        "    ]\n" +
                        "}\n";
        JSONObject dataJson = JSONObject.parseObject(response);
        Map<String, Object> map = dataJson.getInnerMap();
        for (String key : map.keySet()) {
            List value = (List) map.get(key);
            System.out.println("key:" + key);
            for (Object object : value) {
                // 将字符串变为对象
                Gson gson = new Gson();
                LoggerSwitch loggerSwitch = gson.fromJson(object.toString(), LoggerSwitch.class);
                System.out.println("level:" + loggerSwitch.getLevel());
                System.out.println("on:" + loggerSwitch.getOn());
                System.out.println("ipList:" + loggerSwitch.getIpList());
                System.out.println("router:" + loggerSwitch.getRouter());
            }
        }

相关推荐