使用java将json转换为map列表

以下是讲json转换为map列表的代码,在日志中心数据解析过程中,经常会用到这些,特别是将json写入到hbase等非json格式存储系统中,记录下来备忘,供以后使用

maven依赖

       <!--logback start-->
        <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-core -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.2.3</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-access -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-access</artifactId>
            <version>1.2.3</version>
        </dependency>
        <!--logback end-->
        
        <!--json开始-->
        <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>
        <!--json结束-->
package com.juneyao.kc.bigdata.logcenter.example.parse;

import com.google.gson.*;
import org.apache.avro.data.Json;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class JsonParse {
    //日志
    Logger logger = LoggerFactory.getLogger(JsonParse.class);

    //数组json解析
    public List<Map<String, String>> parseJson(String json_data) {

        //如果传入内容为空,则不处理
        if (json_data == null) {
            logger.warn("json is null");
            return null;
        }
        //判断最外层是否为数组
        if (json_data.trim().indexOf("[") == 0) {
            return parseJsonArray(json_data);
        } else {
            return parseJsonObject(json_data);
        }
    }

    //数组json数组解析
    public List<Map<String, String>> parseJsonArray(String json_data) {

        //如果传入内容为空,则不处理
        if (json_data == null) {
            logger.warn("json is null");
            return null;
        }

        List<Map<String, String>> resultMapList = new ArrayList<Map<String, String>>();


        JsonArray jsonArray = null;

        //解析异常处理
        try {
            jsonArray = new JsonParser().parse(json_data).getAsJsonArray();
        } catch (JsonSyntaxException e) {
            logger.error(e.getMessage());
            return null;
        }

        Iterator jsonIteratorArray = jsonArray.iterator();
        while (jsonIteratorArray.hasNext()) {
            Map<String, String> resultMap = new HashMap<String, String>();
            JsonElement jsonElement = (JsonElement) jsonIteratorArray.next();
            JsonObject jsonObject = jsonElement.getAsJsonObject();
            Iterator jsonIterator = jsonObject.keySet().iterator();
            while (jsonIterator.hasNext()) {
                String key = (String) jsonIterator.next();
                JsonElement value = jsonObject.get(key);

                //判断是否有子元素
                if (value.isJsonArray()) {
                    parseJsonArray(key, resultMap, value);
                } else if (value.isJsonObject()) {
                    parseJsonObject(key, resultMap, value);
                } else {
                    resultMap.put(key, value.toString());
                    logger.info("key=" + key + ",value=" + value);
                }
            }
            resultMapList.add(resultMap);
        }

        return resultMapList;
    }

    //数组json解析
    public List<Map<String, String>> parseJsonObject(String json_data) {

        //如果传入内容为空,则不处理
        if (json_data == null) {
            logger.warn("json is null");
            return null;
        }

        List<Map<String, String>> resultMapList = new ArrayList<Map<String, String>>();
        Map<String, String> resultMap = new HashMap<String, String>();


        JsonObject json = null;

        try {
            json = new JsonParser().parse(json_data).getAsJsonObject();
        } catch (JsonSyntaxException e) {
            logger.error(e.fillInStackTrace().toString());
            return null;
        }

        Iterator jsonIterator = json.keySet().iterator();
        while (jsonIterator.hasNext()) {
            String key = (String) jsonIterator.next();
            JsonElement value = json.get(key);
            //判断是否有子元素
            if (value.isJsonArray()) {
                parseJsonArray(key, resultMap, value);
            } else if (value.isJsonObject()) {
                parseJsonObject(key, resultMap, value);
            } else {
                resultMap.put(key, value.toString());
                logger.info("key=" + key + ",value=" + value);
            }
        }


        resultMapList.add(resultMap);

        return resultMapList;
    }

    //数组json解析
    public Map<String, String> parseJsonArray(String parent_key, Map<String, String> resultMap, JsonElement json_data) {

        //判断是否存在key前缀,如果存在,则为前缀赋值
        String key_pre = parent_key != null ? parent_key + "." : "";

        //如果传入内容为空,则不处理
        if (json_data == null) {
            logger.warn("json is null");
            return null;
        }

        JsonArray jsonArray = json_data.getAsJsonArray();
        Iterator jsonIteratorArray = jsonArray.iterator();
        while (jsonIteratorArray.hasNext()) {
            JsonElement jsonElement = (JsonElement) jsonIteratorArray.next();
            JsonObject jsonObject = jsonElement.getAsJsonObject();
            Iterator jsonIterator = jsonObject.keySet().iterator();
            while (jsonIterator.hasNext()) {
                String key = (String) jsonIterator.next();
                JsonElement value = jsonObject.get(key);

                //判断是否有子元素
                if (value.isJsonArray()) {
                    parseJsonArray(key_pre + key, resultMap, value);
                } else if (value.isJsonObject()) {
                    parseJsonObject(key_pre + key, resultMap, value);
                } else {
                    resultMap.put(key_pre + key, value.toString());
                    logger.info("key=" + key_pre + key + ",value=" + value);
                }
            }
        }

        return resultMap;
    }

    //单个json解析
    public Map<String, String> parseJsonObject(String parent_key, Map<String, String> resultMap, JsonElement json_data) {

        //判断是否存在key前缀,如果存在,则为前缀赋值
        String key_pre = parent_key != null ? parent_key + "." : "";

        //如果传入内容为空,则不处理
        if (json_data == null) {
            logger.warn("json is null");
            return null;
        }

        JsonObject json = json_data.getAsJsonObject();


        Iterator jsonIterator = json.keySet().iterator();
        while (jsonIterator.hasNext()) {
            String key = (String) jsonIterator.next();
            JsonElement value = json.get(key);

            //判断是否有子元素
            if (value.isJsonArray()) {
                parseJsonArray(key_pre + key, resultMap, value);
            } else if (value.isJsonObject()) {
                parseJsonObject(key_pre + key, resultMap, value);
            } else {
                resultMap.put(key_pre + key, value.toString());
                logger.info("key=" + key + ",value=" + value);
            }
        }

        return resultMap;
    }


	

    public static void main(String[] args) {

        //输入内容

        //单数据测试
        String log = "  {\n" +
                "    \"traceId\": \"54dffc43f0a06458\",\n" +
                "    \"parentId\": \"2be715d62c3d086d\",\n" +
                "    \"id\": \"9fe0fa4641eb8501\",\n" +
                "    \"kind\": \"CLIENT\",\n" +
                "    \"name\": \"get\",\n" +
                "    \"timestamp\": 1541595389153000,\n" +
                "    \"duration\": 74000,\n" +
                "    \"localEndpoint\": {\n" +
                "      \"serviceName\": \"logcenter-example\",\n" +
                "      \"ipv4\": \"192.168.145.1\"\n" +
                "    },\n" +
                "    \"tags\": [{\n" +
                "      \"http.url1\": \"http://localhost:10603/traceb/b3\"\n" +
                "    },{\n" +
                "      \"http.url2\": \"http://localhost:10603/traceb/b3\"\n" +
                "    },{\n" +
                "      \"http.url3\": \"http://localhost:10603/traceb/b3\"\n" +
                "    },{\n" +
                "      \"http.url4\": \"http://localhost:10603/traceb/b3\"\n" +
                "    }]\n" +
                "  }";

        //错误单数据测试
//        String log="  a{\n" +
//                "    \"traceId\": \"54dffc43f0a06458\",\n" +
//                "    \"parentId\": \"2be715d62c3d086d\",\n" +
//                "    \"id\": \"9fe0fa4641eb8501\",\n" +
//                "    \"kind\": \"CLIENT\",\n" +
//                "    \"name\": \"get\",\n" +
//                "    \"timestamp\": 1541595389153000,\n" +
//                "    \"duration\": 74000,\n" +
//                "    \"localEndpoint\": {\n" +
//                "      \"serviceName\": \"logcenter-example\",\n" +
//                "      \"ipv4\": \"192.168.145.1\"\n" +
//                "    },\n" +
//                "    \"tags\": {\n" +
//                "      \"http.url\": \"http://localhost:10603/traceb/b3\"\n" +
//                "    }\n" +
//                "  }";

        //数组测试
//        String log="[\n" +
//                "  {\n" +
//                "    \"traceId\": \"54dffc43f0a06458\",\n" +
//                "    \"parentId\": \"2be715d62c3d086d\",\n" +
//                "    \"id\": \"9fe0fa4641eb8501\",\n" +
//                "    \"kind\": \"SERVER\",\n" +
//                "    \"name\": \"get\",\n" +
//                "    \"timestamp\": 1541595389180000,\n" +
//                "    \"duration\": 45000,\n" +
//                "    \"localEndpoint\": {\n" +
//                "      \"serviceName\": \"logcenter-example\",\n" +
//                "      \"ipv4\": \"192.168.145.1\"\n" +
//                "    },\n" +
//                "    \"tags\": {\n" +
//                "      \"http.status_code\": \"200\",\n" +
//                "      \"http.url\": \"/traceb/b3\"\n" +
//                "    }\n" +
//                "  },\n" +
//                "  {\n" +
//                "    \"traceId\": \"54dffc43f0a06458\",\n" +
//                "    \"parentId\": \"2be715d62c3d086d\",\n" +
//                "    \"id\": \"9fe0fa4641eb8501\",\n" +
//                "    \"kind\": \"CLIENT\",\n" +
//                "    \"name\": \"get\",\n" +
//                "    \"timestamp\": 1541595389153000,\n" +
//                "    \"duration\": 74000,\n" +
//                "    \"localEndpoint\": {\n" +
//                "      \"serviceName\": \"logcenter-example\",\n" +
//                "      \"ipv4\": \"192.168.145.1\"\n" +
//                "    },\n" +
//                "    \"tags\": {\n" +
//                "      \"http.url\": \"http://localhost:10603/traceb/b3\"\n" +
//                "    }\n" +
//                "  },\n" +
//                "  {\n" +
//                "    \"traceId\": \"54dffc43f0a06458\",\n" +
//                "    \"parentId\": \"5b3985061e0e8dff\",\n" +
//                "    \"id\": \"2be715d62c3d086d\",\n" +
//                "    \"kind\": \"CLIENT\",\n" +
//                "    \"name\": \"get\",\n" +
//                "    \"timestamp\": 1541595389150000,\n" +
//                "    \"duration\": 82000,\n" +
//                "    \"localEndpoint\": {\n" +
//                "      \"serviceName\": \"logcenter-example\",\n" +
//                "      \"ipv4\": \"192.168.145.1\"\n" +
//                "    },\n" +
//                "    \"tags\": [{\n" +
//                "      \"http.url1\": \"http://localhost:10603/traceb/b2\"\n" +
//                "    },{\n" +
//                "      \"http.url2\": \"http://localhost:10603/traceb/b2\"\n" +
//                "    },{\n" +
//                "      \"http.url3\": \"http://localhost:10603/traceb/b2\"\n" +
//                "    }]\n" +
//                "  },\n" +
//                "  {\n" +
//                "    \"traceId\": \"54dffc43f0a06458\",\n" +
//                "    \"parentId\": \"5b3985061e0e8dff\",\n" +
//                "    \"id\": \"2be715d62c3d086d\",\n" +
//                "    \"kind\": \"SERVER\",\n" +
//                "    \"name\": \"get\",\n" +
//                "    \"timestamp\": 1541595389152000,\n" +
//                "    \"duration\": 81000,\n" +
//                "    \"localEndpoint\": {\n" +
//                "      \"serviceName\": \"logcenter-example\",\n" +
//                "      \"ipv4\": \"192.168.145.1\"\n" +
//                "    },\n" +
//                "    \"tags\": {\n" +
//                "      \"http.status_code\": \"200\",\n" +
//                "      \"http.url\": \"/traceb/b2\"\n" +
//                "    }\n" +
//                "  },\n" +
//                "  {\n" +
//                "    \"traceId\": \"54dffc43f0a06458\",\n" +
//                "    \"parentId\": \"a973978a54eae1de\",\n" +
//                "    \"id\": \"5b3985061e0e8dff\",\n" +
//                "    \"kind\": \"SERVER\",\n" +
//                "    \"name\": \"get\",\n" +
//                "    \"timestamp\": 1541595389149000,\n" +
//                "    \"duration\": 85000,\n" +
//                "    \"localEndpoint\": {\n" +
//                "      \"serviceName\": \"logcenter-example\",\n" +
//                "      \"ipv4\": \"192.168.145.1\"\n" +
//                "    },\n" +
//                "    \"tags\": {\n" +
//                "      \"http.status_code\": \"200\",\n" +
//                "      \"http.url\": \"/traceb/b1\"\n" +
//                "    }\n" +
//                "  },\n" +
//                "  {\n" +
//                "    \"traceId\": \"54dffc43f0a06458\",\n" +
//                "    \"parentId\": \"a973978a54eae1de\",\n" +
//                "    \"id\": \"5b3985061e0e8dff\",\n" +
//                "    \"kind\": \"CLIENT\",\n" +
//                "    \"name\": \"get\",\n" +
//                "    \"timestamp\": 1541595389146000,\n" +
//                "    \"duration\": 88000,\n" +
//                "    \"localEndpoint\": {\n" +
//                "      \"serviceName\": \"logcenter-example\",\n" +
//                "      \"ipv4\": \"192.168.145.1\"\n" +
//                "    },\n" +
//                "    \"tags\": {\n" +
//                "      \"http.url\": \"http://localhost:10603/traceb/b1\"\n" +
//                "    }\n" +
//                "  },\n" +
//                "  {\n" +
//                "    \"traceId\": \"54dffc43f0a06458\",\n" +
//                "    \"parentId\": \"9d2deb2e16e56a7e\",\n" +
//                "    \"id\": \"a973978a54eae1de\",\n" +
//                "    \"kind\": \"CLIENT\",\n" +
//                "    \"name\": \"get\",\n" +
//                "    \"timestamp\": 1541595389140000,\n" +
//                "    \"duration\": 96000,\n" +
//                "    \"localEndpoint\": {\n" +
//                "      \"serviceName\": \"logcenter-example\",\n" +
//                "      \"ipv4\": \"192.168.145.1\"\n" +
//                "    },\n" +
//                "    \"tags\": {\n" +
//                "      \"http.url\": \"http://localhost:10603/traceb/b\"\n" +
//                "    }\n" +
//                "  },\n" +
//                "  {\n" +
//                "    \"traceId\": \"54dffc43f0a06458\",\n" +
//                "    \"parentId\": \"9d2deb2e16e56a7e\",\n" +
//                "    \"id\": \"a973978a54eae1de\",\n" +
//                "    \"kind\": \"SERVER\",\n" +
//                "    \"name\": \"get\",\n" +
//                "    \"timestamp\": 1541595389145000,\n" +
//                "    \"duration\": 91000,\n" +
//                "    \"localEndpoint\": {\n" +
//                "      \"serviceName\": \"logcenter-example\",\n" +
//                "      \"ipv4\": \"192.168.145.1\"\n" +
//                "    },\n" +
//                "    \"tags\": {\n" +
//                "      \"http.status_code\": \"200\",\n" +
//                "      \"http.url\": \"/traceb/b\"\n" +
//                "    }\n" +
//                "  },\n" +
//                "  {\n" +
//                "    \"traceId\": \"54dffc43f0a06458\",\n" +
//                "    \"parentId\": \"ee960d7fcb2c29d7\",\n" +
//                "    \"id\": \"9d2deb2e16e56a7e\",\n" +
//                "    \"kind\": \"CLIENT\",\n" +
//                "    \"name\": \"get\",\n" +
//                "    \"timestamp\": 1541595389134000,\n" +
//                "    \"duration\": 104000,\n" +
//                "    \"localEndpoint\": {\n" +
//                "      \"serviceName\": \"logcenter-example\",\n" +
//                "      \"ipv4\": \"192.168.145.1\"\n" +
//                "    },\n" +
//                "    \"tags\": {\n" +
//                "      \"http.url\": \"http://localhost:10603/tracea/a3\"\n" +
//                "    }\n" +
//                "  },\n" +
//                "  {\n" +
//                "    \"traceId\": \"54dffc43f0a06458\",\n" +
//                "    \"parentId\": \"734982011051d666\",\n" +
//                "    \"id\": \"ee960d7fcb2c29d7\",\n" +
//                "    \"kind\": \"SERVER\",\n" +
//                "    \"name\": \"get\",\n" +
//                "    \"timestamp\": 1541595389134000,\n" +
//                "    \"duration\": 106000,\n" +
//                "    \"localEndpoint\": {\n" +
//                "      \"serviceName\": \"logcenter-example\",\n" +
//                "      \"ipv4\": \"192.168.145.1\"\n" +
//                "    },\n" +
//                "    \"tags\": {\n" +
//                "      \"http.status_code\": \"200\",\n" +
//                "      \"http.url\": \"/tracea/a2\"\n" +
//                "    }\n" +
//                "  },\n" +
//                "  {\n" +
//                "    \"traceId\": \"54dffc43f0a06458\",\n" +
//                "    \"parentId\": \"ee960d7fcb2c29d7\",\n" +
//                "    \"id\": \"9d2deb2e16e56a7e\",\n" +
//                "    \"kind\": \"SERVER\",\n" +
//                "    \"name\": \"get\",\n" +
//                "    \"timestamp\": 1541595389139000,\n" +
//                "    \"duration\": 99000,\n" +
//                "    \"localEndpoint\": {\n" +
//                "      \"serviceName\": \"logcenter-example\",\n" +
//                "      \"ipv4\": \"192.168.145.1\"\n" +
//                "    },\n" +
//                "    \"tags\": {\n" +
//                "      \"http.status_code\": \"200\",\n" +
//                "      \"http.url\": \"/tracea/a3\"\n" +
//                "    }\n" +
//                "  },\n" +
//                "  {\n" +
//                "    \"traceId\": \"54dffc43f0a06458\",\n" +
//                "    \"parentId\": \"734982011051d666\",\n" +
//                "    \"id\": \"ee960d7fcb2c29d7\",\n" +
//                "    \"kind\": \"CLIENT\",\n" +
//                "    \"name\": \"get\",\n" +
//                "    \"timestamp\": 1541595389130000,\n" +
//                "    \"duration\": 110000,\n" +
//                "    \"localEndpoint\": {\n" +
//                "      \"serviceName\": \"logcenter-example\",\n" +
//                "      \"ipv4\": \"192.168.145.1\"\n" +
//                "    },\n" +
//                "    \"tags\": {\n" +
//                "      \"http.url\": \"http://localhost:10603/tracea/a2\"\n" +
//                "    }\n" +
//                "  },\n" +
//                "  {\n" +
//                "    \"traceId\": \"54dffc43f0a06458\",\n" +
//                "    \"parentId\": \"54dffc43f0a06458\",\n" +
//                "    \"id\": \"734982011051d666\",\n" +
//                "    \"kind\": \"CLIENT\",\n" +
//                "    \"name\": \"get\",\n" +
//                "    \"timestamp\": 1541595389060000,\n" +
//                "    \"duration\": 183000,\n" +
//                "    \"localEndpoint\": {\n" +
//                "      \"serviceName\": \"logcenter-example\",\n" +
//                "      \"ipv4\": \"192.168.145.1\"\n" +
//                "    },\n" +
//                "    \"tags\": {\n" +
//                "      \"http.url\": \"http://localhost:10603/tracea/a1\"\n" +
//                "    }\n" +
//                "  },\n" +
//                "  {\n" +
//                "    \"traceId\": \"54dffc43f0a06458\",\n" +
//                "    \"parentId\": \"54dffc43f0a06458\",\n" +
//                "    \"id\": \"734982011051d666\",\n" +
//                "    \"kind\": \"SERVER\",\n" +
//                "    \"name\": \"get\",\n" +
//                "    \"timestamp\": 1541595389129000,\n" +
//                "    \"duration\": 115000,\n" +
//                "    \"localEndpoint\": {\n" +
//                "      \"serviceName\": \"logcenter-example\",\n" +
//                "      \"ipv4\": \"192.168.145.1\"\n" +
//                "    },\n" +
//                "    \"tags\": {\n" +
//                "      \"http.status_code\": \"200\",\n" +
//                "      \"http.url\": \"/tracea/a1\"\n" +
//                "    }\n" +
//                "  },\n" +
//                "  {\n" +
//                "    \"traceId\": \"54dffc43f0a06458\",\n" +
//                "    \"id\": \"54dffc43f0a06458\",\n" +
//                "    \"kind\": \"SERVER\",\n" +
//                "    \"name\": \"get\",\n" +
//                "    \"timestamp\": 1541595388948000,\n" +
//                "    \"duration\": 300000,\n" +
//                "    \"localEndpoint\": {\n" +
//                "      \"serviceName\": \"logcenter-example\",\n" +
//                "      \"ipv4\": \"192.168.145.1\"\n" +
//                "    },\n" +
//                "    \"tags\": {\n" +
//                "      \"http.status_code\": \"200\",\n" +
//                "      \"http.url\": \"/tracea/a\"\n" +
//                "    }\n" +
//                "  }\n" +
//                "]";


        //错误数组测试
//        String log="[\n" +
//                "  {\n" +
//                "    \"traceId\": \"54dffc43f0a06458\",\n" +
//                "    \"parentId\": \"2be715d62c3d086d\",\n" +
//                "    \"id\": \"9fe0fa4641eb8501\",\n" +
//                "    \"kind\": \"SERVER\",\n" +
//                "    \"name\": \"get\",\n" +
//                "    \"timestamp\": 1541595389180000,\n" +
//                "    \"duration\": 45000,\n" +
//                "    \"localEndpoint\": {\n" +
//                "      \"serviceName\": \"logcenter-example\",\n" +
//                "      \"ipv4\": \"192.168.145.1\"\n" +
//                "    },\n" +
//                "    \"tags\": {\n" +
//                "      \"http.status_code\": \"200\",\n" +
//                "      \"http.url\": \"/traceb/b3\"\n" +
//                "    }\n" +
//                "  },\n" +
//                "  {\n" +
//                "    \"traceId\": \"54dffc43f0a06458\",\n" +
//                "    \"parentId\": \"2be715d62c3d086d\",\n" +
//                "    \"id\": \"9fe0fa4641eb8501\",\n" +
//                "    \"kind\": \"CLIENT\",\n" +
//                "    \"name\": \"get\",\n" +
//                "    \"timestamp\": 1541595389153000,\n" +
//                "    \"duration\": 74000,\n" +
//                "    \"localEndpoint\": {\n" +
//                "      \"serviceName\": \"logcenter-example\",\n" +
//                "      \"ipv4\": \"192.168.145.1\"\n" +
//                "    },\n" +
//                "    \"tags\": {\n" +
//                "      \"http.url\": \"http://localhost:10603/traceb/b3\"\n" +
//                "    }\n" +
//                "  },\n" +
//                "  {\n" +
//                "    \"traceId\": \"54dffc43f0a06458\",\n" +
//                "    \"parentId\": \"5b3985061e0e8dff\",\n" +
//                "    \"id\": \"2be715d62c3d086d\",\n" +
//                "    \"kind\": \"CLIENT\",\n" +
//                "    \"name\": \"get\",\n" +
//                "    \"timestamp\": 1541595389150000,\n" +
//                "    \"duration\": 82000,\n" +
//                "    \"localEndpoint\": {\n" +
//                "      \"serviceName\": \"logcenter-example\",\n" +
//                "      \"ipv4\": \"192.168.145.1\"\n" +
//                "    },\n" +
//                "    \"tags\": [{\n" +
//                "      \"http.url1\": \"http://localhost:10603/traceb/b2\"\n" +
//                "    },{\n" +
//                "      \"http.url2\": \"http://localhost:10603/traceb/b2\"\n" +
//                "    },{\n" +
//                "      \"http.url3\": \"http://localhost:10603/traceb/b2\"\n" +
//                "    }]\n" +
//                "  },\n" +
//                "  {\n" +
//                "    \"traceId\": \"54dffc43f0a06458\",\n" +
//                "    \"parentId\": \"5b3985061e0e8dff\",\n" +
//                "    \"id\": \"2be715d62c3d086d\",\n" +
//                "    \"kind\": \"SERVER\",\n" +
//                "    \"name\": \"get\",\n" +
//                "    \"timestamp\": 1541595389152000,\n" +
//                "    \"duration\": 81000,\n" +
//                "    \"localEndpoint\": {\n" +
//                "      \"serviceName\": \"logcenter-example\",\n" +
//                "      \"ipv4\": \"192.168.145.1\"\n" +
//                "    },\n" +
//                "    \"tags\": {\n" +
//                "      \"http.status_code\": \"200\",\n" +
//                "      \"http.url\": \"/traceb/b2\"\n" +
//                "    }\n" +
//                "  },\n" +
//                "  {\n" +
//                "    \"traceId\": \"54dffc43f0a06458\",\n" +
//                "    \"parentId\": \"a973978a54eae1de\",\n" +
//                "    \"id\": \"5b3985061e0e8dff\",\n" +
//                "    \"kind\": \"SERVER\",\n" +
//                "    \"name\": \"get\",\n" +
//                "    \"timestamp\": 1541595389149000,\n" +
//                "    \"duration\": 85000,\n" +
//                "    \"localEndpoint\": {\n" +
//                "      \"serviceName\": \"logcenter-example\",\n" +
//                "      \"ipv4\": \"192.168.145.1\"\n" +
//                "    },\n" +
//                "    \"tags\": {\n" +
//                "      \"http.status_code\": \"200\",\n" +
//                "      \"http.url\": \"/traceb/b1\"\n" +
//                "    }\n" +
//                "  },\n" +
//                "  {\n" +
//                "    \"traceId\": \"54dffc43f0a06458\",\n" +
//                "    \"parentId\": \"a973978a54eae1de\",\n" +
//                "    \"id\": \"5b3985061e0e8dff\",\n" +
//                "    \"kind\": \"CLIENT\",\n" +
//                "    \"name\": \"get\",\n" +
//                "    \"timestamp\": 1541595389146000,\n" +
//                "    \"duration\": 88000,\n" +
//                "    \"localEndpoint\": {\n" +
//                "      \"serviceName\": \"logcenter-example\",\n" +
//                "      \"ipv4\": \"192.168.145.1\"\n" +
//                "    },\n" +
//                "    \"tags\": {\n" +
//                "      \"http.url\": \"http://localhost:10603/traceb/b1\"\n" +
//                "    }\n" +
//                "  },\n" +
//                "  {\n" +
//                "    \"traceId\": \"54dffc43f0a06458\",\n" +
//                "    \"parentId\": \"9d2deb2e16e56a7e\",\n" +
//                "    \"id\": \"a973978a54eae1de\",\n" +
//                "    \"kind\": \"CLIENT\",\n" +
//                "    \"name\": \"get\",\n" +
//                "    \"timestamp\": 1541595389140000,\n" +
//                "    \"duration\": 96000,\n" +
//                "    \"localEndpoint\": {\n" +
//                "      \"serviceName\": \"logcenter-example\",\n" +
//                "      \"ipv4\": \"192.168.145.1\"\n" +
//                "    },\n" +
//                "    \"tags\": {\n" +
//                "      \"http.url\": \"http://localhost:10603/traceb/b\"\n" +
//                "    }\n" +
//                "  },\n" +
//                "  {\n" +
//                "    \"traceId\": \"54dffc43f0a06458\",\n" +
//                "    \"parentId\": \"9d2deb2e16e56a7e\",\n" +
//                "    \"id\": \"a973978a54eae1de\",\n" +
//                "    \"kind\": \"SERVER\",\n" +
//                "    \"name\": \"get\",\n" +
//                "    \"timestamp\": 1541595389145000,\n" +
//                "    \"duration\": 91000,\n" +
//                "    \"localEndpoint\": {\n" +
//                "      \"serviceName\": \"logcenter-example\",\n" +
//                "      \"ipv4\": \"192.168.145.1\"\n" +
//                "    },\n" +
//                "    \"tags\": {\n" +
//                "      \"http.status_code\": \"200\",\n" +
//                "      \"http.url\": \"/traceb/b\"\n" +
//                "    }\n" +
//                "  },\n" +
//                "  {\n" +
//                "    \"traceId\": \"54dffc43f0a06458\",\n" +
//                "    \"parentId\": \"ee960d7fcb2c29d7\",\n" +
//                "    \"id\": \"9d2deb2e16e56a7e\",\n" +
//                "    \"kind\": \"CLIENT\",\n" +
//                "    \"name\": \"get\",\n" +
//                "    \"timestamp\": 1541595389134000,\n" +
//                "    \"duration\": 104000,\n" +
//                "    \"localEndpoint\": {\n" +
//                "      \"serviceName\": \"logcenter-example\",\n" +
//                "      \"ipv4\": \"192.168.145.1\"\n" +
//                "    },\n" +
//                "    \"tags\": {\n" +
//                "      \"http.url\": \"http://localhost:10603/tracea/a3\"\n" +
//                "    }\n" +
//                "  },\n" +
//                "  {\n" +
//                "    \"traceId\": \"54dffc43f0a06458\",\n" +
//                "    \"parentId\": \"734982011051d666\",\n" +
//                "    \"id\": \"ee960d7fcb2c29d7\",\n" +
//                "    \"kind\": \"SERVER\",\n" +
//                "    \"name\": \"get\",\n" +
//                "    \"timestamp\": 1541595389134000,\n" +
//                "    \"duration\": 106000,\n" +
//                "    \"localEndpoint\": {\n" +
//                "      \"serviceName\": \"logcenter-example\",\n" +
//                "      \"ipv4\": \"192.168.145.1\"\n" +
//                "    },\n" +
//                "    \"tags\": {\n" +
//                "      \"http.status_code\": \"200\",\n" +
//                "      \"http.url\": \"/tracea/a2\"\n" +
//                "    }\n" +
//                "  },\n" +
//                "  {\n" +
//                "    \"traceId\": \"54dffc43f0a06458\",\n" +
//                "    \"parentId\": \"ee960d7fcb2c29d7\",\n" +
//                "    \"id\": \"9d2deb2e16e56a7e\",\n" +
//                "    \"kind\": \"SERVER\",\n" +
//                "    \"name\": \"get\",\n" +
//                "    \"timestamp\": 1541595389139000,\n" +
//                "    \"duration\": 99000,\n" +
//                "    \"localEndpoint\": {\n" +
//                "      \"serviceName\": \"logcenter-example\",\n" +
//                "      \"ipv4\": \"192.168.145.1\"\n" +
//                "    },\n" +
//                "    \"tags\": {\n" +
//                "      \"http.status_code\": \"200\",\n" +
//                "      \"http.url\": \"/tracea/a3\"\n" +
//                "    }\n" +
//                "  },\n" +
//                "  {\n" +
//                "    \"traceId\": \"54dffc43f0a06458\",\n" +
//                "    \"parentId\": \"734982011051d666\",\n" +
//                "    \"id\": \"ee960d7fcb2c29d7\",\n" +
//                "    \"kind\": \"CLIENT\",\n" +
//                "    \"name\": \"get\",\n" +
//                "    \"timestamp\": 1541595389130000,\n" +
//                "    \"duration\": 110000,\n" +
//                "    \"localEndpoint\": {\n" +
//                "      \"serviceName\": \"logcenter-example\",\n" +
//                "      \"ipv4\": \"192.168.145.1\"\n" +
//                "    },\n" +
//                "    \"tags\": {\n" +
//                "      \"http.url\": \"http://localhost:10603/tracea/a2\"\n" +
//                "    }\n" +
//                "  },\n" +
//                "  {\n" +
//                "    \"traceId\": \"54dffc43f0a06458\",\n" +
//                "    \"parentId\": \"54dffc43f0a06458\",\n" +
//                "    \"id\": \"734982011051d666\",\n" +
//                "    \"kind\": \"CLIENT\",\n" +
//                "    \"name\": \"get\",\n" +
//                "    \"timestamp\": 1541595389060000,\n" +
//                "    \"duration\": 183000,\n" +
//                "    \"localEndpoint\": {\n" +
//                "      \"serviceName\": \"logcenter-example\",\n" +
//                "      \"ipv4\": \"192.168.145.1\"\n" +
//                "    },\n" +
//                "    \"tags\": {\n" +
//                "      \"http.url\": \"http://localhost:10603/tracea/a1\"\n" +
//                "    }\n" +
//                "  },\n" +
//                "  {\n" +
//                "    \"traceId\": \"54dffc43f0a06458\",\n" +
//                "    \"parentId\": \"54dffc43f0a06458\",\n" +
//                "    \"id\": \"734982011051d666\",\n" +
//                "    \"kind\": \"SERVER\",\n" +
//                "    \"name\": \"get\",\n" +
//                "    \"timestamp\": 1541595389129000,\n" +
//                "    \"duration\": 115000,\n" +
//                "    \"localEndpoint\": {\n" +
//                "      \"serviceName\": \"logcenter-example\",\n" +
//                "      \"ipv4\": \"192.168.145.1\"\n" +
//                "    },\n" +
//                "    \"tags\": {\n" +
//                "      \"http.status_code\": \"200\",\n" +
//                "      \"http.url\": \"/tracea/a1\"\n" +
//                "    }\n" +
//                "  },\n" +
//                "  {\n" +
//                "    \"traceId\": \"54dffc43f0a06458\",\n" +
//                "    \"id\": \"54dffc43f0a06458\",\n" +
//                "    \"kind\": \"SERVER\",\n" +
//                "    \"name\": \"get\",\n" +
//                "    \"timestamp\": 1541595388948000,\n" +
//                "    \"duration\": 300000,\n" +
//                "    \"localEndpoint\": {\n" +
//                "      \"serviceName\": \"logcenter-example\",\n" +
//                "      \"ipv4\": \"192.168.145.1\"\n" +
//                "    },\n" +
//                "    \"tags\": {\n" +
//                "      \"http.status_code\": \"200\",\n" +
//                "      \"http.url\": \"/tracea/a\"\n" +
//                "    }\n" +
//                "  }\n" +
//                "a]a";


        //将json内容打印到控制台
//        System.out.println(log);

        //解析json
        JsonParse jsonParse = new JsonParse();
        List<Map<String, String>> resultMqp = jsonParse.parseJson(log);

        //遍历展示解析后的json
        if (resultMqp != null)
            for (Map<String, String> map : resultMqp) {
                Iterator iteratorMap = map.keySet().iterator();
                String key;
                while (iteratorMap.hasNext()) {
                    key = (String) iteratorMap.next();
                    System.out.println("key=" + key + ",value=" + map.get(key));
                }
            }

    }

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时空琴弦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值