package ****
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
public class AssembleTargetJson {
public static void main(String[] args) {
String s = "{\"proposal1\":{\"demo5\":{\"amvehicle\":{\"vehicleClassCode\":\"J\"}}}}";
String s2 = "{\"proposal1\":{\"amvehicle1\":[{\"checkType\":\"2222\",\"querySequenceNo\":\"342423423432\"},{\"checkType\":\"2222\",\"querySequenceNo\":\"342423423432\"}]}}\n";
String s3 = "{\"proposal2\":{\"demo\":{\"pasepart2\":{\"querySequenceNo\":\"342423423432\",\"checkType\":\"2222\"}}}}\n";
String s4 = "{\"proposal3\":{\"demo\":{\"pasepart2\":{\"querySequenceNo\":\"342423423432\",\"checkType\":\"2222\"}}}}\n";
String s5 = "{\"proposal1\":{\"demo5\":{\"amvehicle2\":{\"querySequenceNo\":\"342423423432\",\"checkType\":\"2222\"}}}}\n";
JSONObject jsonObject = JSONObject.parseObject(s);
JSONObject jsonObject2 = JSONObject.parseObject(s2);
JSONObject jsonObject3 = JSONObject.parseObject(s3);
JSONObject jsonObject4 = JSONObject.parseObject(s4);
JSONObject jsonObject5 = JSONObject.parseObject(s5);
ArrayList<JSONObject> list = new ArrayList<>();
list.add(jsonObject);
list.add(jsonObject2);
list.add(jsonObject3);
list.add(jsonObject4);
list.add(jsonObject5);
JSONObject jsonObject1 = list.get(0);
for (int i = 0; i < list.size() - 1; i++) {
AssemblyNotSameJson(jsonObject1, list.get(i + 1), i + 1);
}
System.out.println(jsonObject1);
}
/**
* @param jsonObject1
* @param jsonObject
* @param i
* @description: 组装第一个json和第二个json
* @return: void
* @author: 齐天铭
* @time: 2021/7/10 3:27 下午
*/
private static void AssemblyNotSameJson(JSONObject jsonObject1, JSONObject jsonObject, int i) {
for (Map.Entry<String, Object> node2 : jsonObject.entrySet()) {
Object x = jsonObject1.get(node2.getKey());
// 判断如果第一个json和第二个json如果第一层都不同,直接插入到第一个json中
if (x == null) {
jsonObject1.put(node2.getKey(), jsonObject.get(node2.getKey()));
} else {
RecursionAssemblyJson(jsonObject1, jsonObject, new JSONObject());
}
}
}
/**
* @param firstJson
* @param secondJson
* @param lastFirstJson 存放上一次json 插入本次不同都数据
* @description: 递归组装json
* @return: void
* @author: 齐天铭
* @time: 2021/7/10 3:28 下午
*/
private static void RecursionAssemblyJson(JSONObject firstJson, JSONObject secondJson, JSONObject lastFirstJson) {
Iterator<String> iterator = firstJson.keySet().iterator();
Iterator<String> secondIterator = secondJson.keySet().iterator();
String secondKey = "";
Object secondValue = "";
JSONArray jsonArray = new JSONArray();
// 每次第二个josn都只取一层,为了和第一层比较
while (secondIterator.hasNext()) {
secondKey = secondIterator.next();
secondValue = secondJson.get(secondKey);
// 判断是json还是jsonArray
if (isJsonObject(secondJson, secondKey)) {
secondJson = secondJson.getJSONObject(secondKey);
} else if (isJsonArray(secondJson, secondKey)) {
jsonArray = secondJson.getJSONArray(secondKey);
}
break;
}
// 循环查询第一个json里面属性信息
while (iterator.hasNext()) {
String firstKey = iterator.next();
if (isJsonObject(firstJson, firstKey)) {
// 通过key解析当前属性信息
JSONObject jsonObject3 = firstJson.getJSONObject(firstKey);
if (firstKey.equals(secondKey)) {
// 如果当前属性值相同,那就查找下一个
RecursionAssemblyJson(jsonObject3, secondJson, jsonObject3);
} else {
if (jsonArray != null && jsonArray.size() > 0) {
lastFirstJson.put(secondKey, jsonArray);
} else {
lastFirstJson.put(secondKey, secondValue);
}
}
}
}
}
/**
* @param jsonObject,key
* @Description: 判断是否为json
* @return: java.lang.Boolean
* @author: 齐天铭
* @time: 2021/6/29 11:42 上午
*/
public static Boolean isJsonObject(JSONObject jsonObject, String key) {
try {
JSONObject jsonObject2 = jsonObject.getJSONObject(key);
return true;
} catch (
Exception e) {
return false;
}
}
/**
* @param jsonObject,key
* @description: 判断是否为jsonarray
* @return: java.lang.Boolean
* @author: 齐天铭
* @time: 2021/6/29 11:42 上午
*/
public static Boolean isJsonArray(JSONObject jsonObject, String key) {
try {
JSONArray jsonArray = jsonObject.getJSONArray(key);
// JSONArray objects = JSONObject.parseArray(jsonStringing);
return true;
} catch (Exception e) {
return false;
}
}
}
多个嵌套Json合并
最新推荐文章于 2022-12-17 15:59:23 发布