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