栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

json平铺

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

json平铺

import net.sf.json.JSONObject;
import com.google.gson.*;
import org.apache.commons.lang.StringUtils;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;


public class EsHitsUtils {
    
    public static JSONObject jsonFormatter(String jsonStr) {

        Map map = new LinkedHashMap<>();
        parseJson2Map(map, jsonStr, null);
        JSONObject jsonObject = JSONObject.fromObject(map);
    
        return jsonObject;
    }

    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() + StringUtils.capitalize(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();
                int index = 1;
                while (iterator.hasNext()) {
                    JsonElement jsonElement1 = iterator.next();
                    try {
                        jsonElement1.getAsJsonObject();
                    } catch (IllegalStateException ie) {
                        map.put(fullkey + index++, jsonElement1.getAsString());
                        continue;
                    }
                    parseJson2Map(map, jsonElement1.getAsJsonObject(), fullkey + index++);
                }
                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());
                }
            }
        }
    }

    
    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 = "[{"code":1,"msg":[{"a":"v","c":"d"},{"a":"v","c":"d"}],"msg1":["fasfsf","3113"]}]";
        System.out.println(jsonFormatter(json));
    }

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1020620.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号