json多层嵌套,转成一层

package com.ruoyi.common.utils;

import net.sf.json.JSONObject;
import com.google.gson.*;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 多层嵌套json数据转换为单层,同时规格化
 **/
public class EsHitsUtils {
    /**
     *把拍平后的json进行格式化处理,输出标准的json格式
     * @param uglyJSONString
     * @return
     */
    public static String jsonFormatter(String uglyJSONString){

        Map map = new HashMap<>();
        parseJson2Map(map,uglyJSONString,null);
        JSONObject jsonObject = JSONObject.fromObject(map);
        uglyJSONString = jsonObject.toString();

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        JsonParser jp = new JsonParser();
        JsonElement je = jp.parse(uglyJSONString);
        String prettyJsonString = gson.toJson(je);
        System.out.println(prettyJsonString);
        return "";
    }

    public static void parseJson2Map(Map map,JsonObject jsonObject,String parentKey){
        for (Map.Entry object : jsonObject.entrySet()) {
            String key = object.getKey();
            JsonElement value = object.getValue();
            String fullkey = (null == parentKey || parentKey.trim().equals("")) ? key : parentKey.trim() + "." + key;
            //判断对象的类型,如果是空类型则安装空类型处理
            if (value.isJsonNull()){
                map.put(fullkey,null);
                continue;
                //如果是JsonObject对象则递归处理
            }else if (value.isJsonObject()){
                parseJson2Map(map,value.getAsJsonObject(),fullkey);
                //如果是JsonArray数组则迭代,然后进行递归
            }else if (value.isJsonArray()){
                JsonArray jsonArray = value.getAsJsonArray();
                Iterator iterator = jsonArray.iterator();
                while (iterator.hasNext()) {
                    JsonElement jsonElement1 = iterator.next();
                    try {
                        jsonElement1.getAsJsonObject();
                    }catch (IllegalStateException ie){
                        map.put(fullkey,jsonElement1.getAsString());
                        continue;
                    }
                    parseJson2Map(map, jsonElement1.getAsJsonObject(), fullkey);
                }
                continue;
                // 如果是JsonPrimitive对象则获取当中的值,则还需要再次进行判断一下
            }else if (value.isJsonPrimitive()){
                try {
                    JsonElement element = new JsonParser().parse(value.getAsString());
                    if (element.isJsonNull()){
                        map.put(fullkey,value.getAsString());
                    }else if (element.isJsonObject()) {
                        parseJson2Map(map, element.getAsJsonObject(), fullkey);
                    } else if (element.isJsonPrimitive()) {
                        JsonPrimitive jsonPrimitive = element.getAsJsonPrimitive();

                        if (jsonPrimitive.isNumber()) {
                            map.put(fullkey, jsonPrimitive.getAsNumber());
                        } else {
                            map.put(fullkey, jsonPrimitive.getAsString());
                        }
                    } else if (element.isJsonArray()) {
                        JsonArray jsonArray = element.getAsJsonArray();
                        Iterator iterator = jsonArray.iterator();
                        while (iterator.hasNext()) {
                            parseJson2Map(map, iterator.next().getAsJsonObject(), fullkey);
                        }
                    }
                }catch (Exception e){
                    map.put(fullkey,value.getAsString());
                }
            }
        }
    }

    /**
     * 使用Gson拍平json字符串,即当有多层json嵌套时,可以把多层的json拍平为一层
     * @param map
     * @param json
     * @param parentKey
     */
    public static void parseJson2Map(Map map, String json, String parentKey){
        JsonElement jsonElement = new JsonParser().parse(json);
        if (jsonElement.isJsonObject()) {
            JsonObject jsonObject = jsonElement.getAsJsonObject();
            parseJson2Map(map,jsonObject,parentKey);
            //传入的还是一个json数组
        }else if (jsonElement.isJsonArray()){
            JsonArray jsonArray = jsonElement.getAsJsonArray();
            Iterator iterator = jsonArray.iterator();
            while (iterator.hasNext()){
                parseJson2Map(map,iterator.next().getAsJsonObject(),parentKey);
            }
        }else if (jsonElement.isJsonPrimitive()){
            System.out.println("please check the json format!");
        }else if (jsonElement.isJsonNull()){

        }
    }
    public static void main(String[] args){
        String json = "{\n" +
                "  \"_index\" : \"tomcat1-7.7.1-2020.06.22\",\n" +
                "  \"_type\" : \"_doc\",\n" +
                "  \"_id\" : \"F4vf2nIBOcu5RyqDQ6_a\",\n" +
                "  \"_score\" : 1.0,\n" +
                "  \"_source\" : {\n" +
                "    \"@timestamp\" : \"2020-06-22T07:12:43.505Z\",\n" +
                "    \"log\" : {\n" +
                "      \"offset\" : 146,\n" +
                "      \"file\" : {\n" +
                "        \"path\" : \"/root/apache-tomcat-8.5.16/logs/localhost_access_log.2020-06-22.txt\"\n" +
                "      }\n" +
                "    },\n" +
                "    \"message\" : \"192.168.81.1 - - [22/Jun/2020:00:12:33 -0700] \\\"GET /examples/ HTTP/1.1\\\" 304 -\",\n" +
                "    \"tags\" : [\n" +
                "      \"tomcat1\"\n" +
                "    ],\n" +
                "    \"input\" : {\n" +
                "      \"type\" : \"log\"\n" +
                "    },\n" +
                "    \"host\" : {\n" +
                "      \"architecture\" : \"x86_64\",\n" +
                "      \"os\" : {\n" +
                "        \"version\" : \"7 (Core)\",\n" +
                "        \"family\" : \"redhat\",\n" +
                "        \"name\" : \"CentOS Linux\",\n" +
                "        \"kernel\" : \"3.10.0-514.el7.x86_64\",\n" +
                "        \"codename\" : \"Core\",\n" +
                "        \"platform\" : \"centos\"\n" +
                "      },\n" +
                "      \"name\" : \"localhost.localdomain\",\n" +
                "      \"id\" : \"3ead4e1cf63c492c9899a03473532132\",\n" +
                "      \"containerized\" : false,\n" +
                "      \"ip\" : [\n" +
                "        \"192.168.81.129\",\n" +
                "        \"fe80::e381:bd93:fc44:e18c\",\n" +
                "        \"192.168.122.1\"\n" +
                "      ],\n" +
                "      \"mac\" : [\n" +
                "        \"00:0c:29:93:18:9c\",\n" +
                "        \"52:54:00:5c:62:5b\",\n" +
                "        \"52:54:00:5c:62:5b\"\n" +
                "      ],\n" +
                "      \"hostname\" : \"localhost.localdomain\"\n" +
                "    },\n" +
                "    \"agent\" : {\n" +
                "      \"hostname\" : \"localhost.localdomain\",\n" +
                "      \"id\" : \"a3236c26-74e9-45ad-9e2e-e49184e17812\",\n" +
                "      \"version\" : \"7.7.1\",\n" +
                "      \"type\" : \"filebeat\",\n" +
                "      \"ephemeral_id\" : \"e8efb94d-553d-42b7-a18f-060c398a1b81\"\n" +
                "    },\n" +
                "    \"ecs\" : {\n" +
                "      \"version\" : \"1.5.0\"\n" +
                "    }\n" +
                "  }\n" +
                "}";
        System.out.println(jsonFormatter(json));
    }

}

输入结果如下: 

{
  "_index": "tomcat1-7.7.1-2020.06.22",
  "_source.host.ip": "192.168.122.1",
  "_source.host.os.family": "redhat",
  "_score": 1,
  "_source.host.name": "localhost.localdomain",
  "_source.agent.type": "filebeat",
  "_source.host.id": "3ead4e1cf63c492c9899a03473532132",
  "_source.input.type": "log",
  "_source.log.offset": 146,
  "_source.host.os.platform": "centos",
  "_source.agent.ephemeral_id": "e8efb94d-553d-42b7-a18f-060c398a1b81",
  "_source.agent.hostname": "localhost.localdomain",
  "_source.tags": "tomcat1",
  "_source.host.containerized": "false",
  "_type": "_doc",
  "_source.host.mac": "52:54:00:5c:62:5b",
  "_source.host.os.version": "7 (Core)",
  "_source.agent.version": "7.7.1",
  "_source.host.hostname": "localhost.localdomain",
  "_source.host.os.kernel": "3.10.0-514.el7.x86_64",
  "_source.host.os.name": "CentOS Linux",
  "_source.host.os.codename": "Core",
  "_source.log.file.path": "/root/apache-tomcat-8.5.16/logs/localhost_access_log.2020-06-22.txt",
  "_source.host.architecture": "x86_64",
  "_source.@timestamp": "2020-06-22T07:12:43.505Z",
  "_source.agent.id": "a3236c26-74e9-45ad-9e2e-e49184e17812",
  "_id": "F4vf2nIBOcu5RyqDQ6_a",
  "_source.ecs.version": "1.5.0",
  "_source.message": "192.168.81.1 - - [22/Jun/2020:00:12:33 -0700] \"GET /examples/ HTTP/1.1\" 304 -"
}

 


			com.google.code.gson
			gson
			2.8.2
		
		
			net.sf.json-lib
			json-lib
			2.4
			jdk15
		

 

你可能感兴趣的:(java)