在 Java 中使用 Gson 合并/扩展 JSON 对象
Posted
技术标签:
【中文标题】在 Java 中使用 Gson 合并/扩展 JSON 对象【英文标题】:Merge/Extend JSON Objects using Gson in Java 【发布时间】:2016-03-09 14:58:30 【问题描述】:通常,我需要合并两个 JSON 对象(类似于 jQuery 的 $.extend()
的工作方式)。但是,Gson 库没有内置功能,他们说they won't implement it。
做类似的事情:
private void merge(JsonObject firstObj, JsonObject secondObj)
for(String keyInSecondObj : secondObj.entrySet().keySet())
if(!firstObj.has(keyInSecondObj ))
firstObj.add(secondMap.get(keyInSecondObj));
太简单了,因为它不处理递归合并 JsonObjects,不处理两个 map 中都存在键时的冲突,并且对数组等非原始值没有特殊处理。
我没有找到任何预先构建的解决方案来执行此操作。我宁愿使用经过彻底测试的东西,而不是编写自己的方法,但它必须是 Gson(不是 Jackson 或其他)。
编辑:我最终编写了自己的实现,并添加为answer to this question
This question 不是重复的,因为它没有使用 Gson(或 Java)。
【问题讨论】:
这很相似,但不使用 Gson 库:***.com/questions/21160337/… 请先阅读标签摘要,然后再将它们添加到您的问题中...... @JarrodRoberson 这个问题不是骗人的!请看我的解释。标记的重复项甚至没有使用 Java 这在几乎所有方面都是离题,没有代码,寻求建议,过于宽泛和基于意见。由于前面提到的偏离主题的原因,在最一般意义上的方法中也是重复的。对于这个糟糕的问题,一个离题的原因和另一个一样好,因为我们不能因为多种原因不提名。 @JarrodRoberson 我有一个关于此功能的特定 Gson 库信息的链接,并且有我自己的代码作为该问题的建议解决方案(以帮助社区)。有没有更好的方法或者我应该避免尝试这样做? 【参考方案1】:这是我第一次尝试编写自己的静态合并方法。随意戳它的洞。
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.util.Map;
public class GsonTools
public static enum ConflictStrategy
THROW_EXCEPTION, PREFER_FIRST_OBJ, PREFER_SECOND_OBJ, PREFER_NON_NULL;
public static class JsonObjectExtensionConflictException extends Exception
public JsonObjectExtensionConflictException(String message)
super(message);
public static void extendJsonObject(JsonObject destinationObject, ConflictStrategy conflictResolutionStrategy, JsonObject ... objs)
throws JsonObjectExtensionConflictException
for (JsonObject obj : objs)
extendJsonObject(destinationObject, obj, conflictResolutionStrategy);
private static void extendJsonObject(JsonObject leftObj, JsonObject rightObj, ConflictStrategy conflictStrategy)
throws JsonObjectExtensionConflictException
for (Map.Entry<String, JsonElement> rightEntry : rightObj.entrySet())
String rightKey = rightEntry.getKey();
JsonElement rightVal = rightEntry.getValue();
if (leftObj.has(rightKey))
//conflict
JsonElement leftVal = leftObj.get(rightKey);
if (leftVal.isJsonArray() && rightVal.isJsonArray())
JsonArray leftArr = leftVal.getAsJsonArray();
JsonArray rightArr = rightVal.getAsJsonArray();
//concat the arrays -- there cannot be a conflict in an array, it's just a collection of stuff
for (int i = 0; i < rightArr.size(); i++)
leftArr.add(rightArr.get(i));
else if (leftVal.isJsonObject() && rightVal.isJsonObject())
//recursive merging
extendJsonObject(leftVal.getAsJsonObject(), rightVal.getAsJsonObject(), conflictStrategy);
else //not both arrays or objects, normal merge with conflict resolution
handleMergeConflict(rightKey, leftObj, leftVal, rightVal, conflictStrategy);
else //no conflict, add to the object
leftObj.add(rightKey, rightVal);
private static void handleMergeConflict(String key, JsonObject leftObj, JsonElement leftVal, JsonElement rightVal, ConflictStrategy conflictStrategy)
throws JsonObjectExtensionConflictException
switch (conflictStrategy)
case PREFER_FIRST_OBJ:
break;//do nothing, the right val gets thrown out
case PREFER_SECOND_OBJ:
leftObj.add(key, rightVal);//right side auto-wins, replace left val with its val
break;
case PREFER_NON_NULL:
//check if right side is not null, and left side is null, in which case we use the right val
if (leftVal.isJsonNull() && !rightVal.isJsonNull())
leftObj.add(key, rightVal);
//else do nothing since either the left value is non-null or the right value is null
break;
case THROW_EXCEPTION:
throw new JsonObjectExtensionConflictException("Key " + key + " exists in both objects and the conflict resolution strategy is " + conflictStrategy);
default:
throw new UnsupportedOperationException("The conflict strategy " + conflictStrategy + " is unknown and cannot be processed");
【讨论】:
我已投票决定重新提出您的原始问题。 ***.com/questions/34092373/…我也遇到了类似的问题,选择使用github.com/fge/json-patch/blob/master/README.md#json-diff 感谢您的实施!这看起来很棒!会尝试一下,看看它是否适合我的需求。【参考方案2】:你可以使用
Map firstObject = new GSON().fromJson(json1, HashMap.class);
Map secondObject = new GSON().fromJson(json2, HashMap.class);
// 根据需要合并 Map firstObject 和 secondObject,见this post
String resultJson = new GSON().toJson(resultMap);
【讨论】:
这种策略很幼稚,因为它只是将第二个映射中存在的任何值添加到第一个映射。如果存在冲突(两个对象中都存在),或者值是非原始的(递归对象合并和数组连接),它不处理该怎么做 可以,但是你可以手动合并 conflit,只需要合并两个 HashMap 手工!?真的吗?【参考方案3】:您可以将一个 jsonobject 转换为 jsonaray,(您可以检查 here) 然后你可以将新的 jsonarray 添加到另一个 jsonobject 或将两个 jsonobject 转换为 jsonarray,然后创建一个新的 jsonobject。
jsonObject.add("property", jsonarray);
【讨论】:
【参考方案4】:下面的库类允许数组和对象的深度合并。他们按照描述应用策略,但您可以通过更改方法中间的简单操作来采用自己的策略。
合并(覆盖或添加)的“覆盖”策略:
原始类型字段被覆盖。 具有相同声明键的对象被合并。 如果对象内容相等,则合并没有声明键的对象。 其他对象被覆盖或添加。 数组合并:在同一个数组中添加重复项,直到两个数组中此类项的数量相等。用法(String
或GsonJsonObject
/JsonArray
可以返回):
// Straightforward object merging:
Json.mergeObjects(
"my_object_as_string",
"my_other_object_as_string");
// Merge "my_objects" arrays and set object identity keys:
HashMap<String, String[]> keyCombinations = new HashMap<>();
keyCombinations.put(
"objects",
new String[] "object_identity_key_one", "object_identity_key_two");
Json.mergeArrays(
"my_objects",
keyCombinations,
"[my_array_as_string]",
"[my_other_array_as_string]"));
图书馆类:
package com.example.utils;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.util.ArrayList;
import java.util.HashMap;
import io.reactivex.annotations.NonNull;
@SuppressWarnings("unused")
public class Json
/**
* Merge given JSON-objects. Same keys are merged for objects and
* overwritten by last object for primitive types.
*
* @param keyCombinations Key names for unique object identification.
* Or empty collection.
* @param objects Any amount of JSON-objects to merge.
*
* @return Merged JSON-object.
*/
public static JsonObject mergeObjects(
@NonNull
HashMap<String, String[]> keyCombinations,
Object... objects)
JsonObject mergedObject = new JsonObject();
for (Object object : objects)
JsonObject jsonObject = (JsonObject) object;
for (String key : jsonObject.keySet())
JsonElement parameter = jsonObject.get(key);
if (mergedObject.has(key))
// Key name matches:
if (jsonObject.get(key).isJsonObject())
// This is object - merge:
parameter =
mergeObjects(
keyCombinations,
mergedObject.get(key).getAsJsonObject(),
jsonObject.get(key).getAsJsonObject());
else if (jsonObject.get(key).isJsonArray())
// This is array - merge:
parameter =
mergeArrays(
key,
keyCombinations,
mergedObject.get(key).getAsJsonArray(),
jsonObject.get(key).getAsJsonArray());
else
// This is neither object nor array - replace value:
mergedObject.add(key, parameter);
// No such field yet - add:
mergedObject.add(key, parameter);
return mergedObject;
/**
* Alternative - no object identity keys are set.
* See @link Json#mergeObjects(HashMap, Object...)
*/
public static JsonObject mergeObjects(
Object... objects)
return (
mergeObjects(
new HashMap<>(),
objects));
/**
* Get GSON-object from string.
*
* @param jsonString JSON-object as string.
*
* @return JsonObject (GSON).
*/
public static JsonObject getJsonObject(String jsonString)
JsonObject jsonObject = new JsonObject();
JsonParser parser;
parser = new JsonParser();
if (jsonString != null)
jsonObject =
parser
.parse(
jsonString)
.getAsJsonObject();
return jsonObject;
/**
* See @link Json#mergeObjects(HashMap, Object...)
*/
public static String mergeObjects(
HashMap<String, String[]> keyCombinations,
String... jsonObjects)
ArrayList<JsonObject> objects = new ArrayList<>();
for (String jsonObject : jsonObjects)
objects.add(
Json2.getJsonObject(jsonObject));
return (
mergeObjects(
keyCombinations,
objects.toArray())
.toString());
/**
* Alternative - no object identity keys are set.
* See @link Json#mergeObjects(HashMap, Object...)
*/
public static String mergeObjects(
String... jsonObjects)
ArrayList<JsonObject> objects = new ArrayList<>();
for (String jsonObject : jsonObjects)
objects.add(
getJsonObject(jsonObject));
return (
mergeObjects(
new HashMap<>(),
objects.toArray())
.toString());
/**
* See @link Json#mergeArrays(String, HashMap, Object...)
*/
public static String mergeArrays(
String arrayName,
HashMap<String, String[]> keyCombinations,
String... jsonArrays)
ArrayList<JsonArray> arrays = new ArrayList<>();
for (String jsonArray : jsonArrays)
arrays.add(
getJsonArray(jsonArray));
return (
mergeArrays(
arrayName,
keyCombinations,
arrays.toArray())
.toString());
/**
* Alternative - no object identity keys are set.
* See @link Json#mergeArrays(String, HashMap, Object...)
*/
public static String mergeArrays(
String... jsonArrays)
ArrayList<JsonArray> arrays = new ArrayList<>();
for (String jsonArray : jsonArrays)
arrays.add(
getJsonArray(jsonArray));
return (
mergeArrays(
"",
new HashMap<>(),
arrays.toArray())
.toString());
/**
* Alternative - no object identity keys are set.
* Seee @link Json#mergeArrays(String, HashMap, Object...)
*/
public static JsonArray mergeArrays(
Object... jsonArrays)
return (
mergeArrays(
"",
new HashMap<>(),
jsonArrays));
/**
* Merge arrays following "Overlay" strategy (overwrite or add).
* Duplicate elements are added to array until their amount is equal
* in both arrays. Objects are considered identical if their
* identifier-keys are present and their values are equal. If no such
* keys, then objects are considered identical on equal content.
*
* @param arrayName Merged arrays name or empty string.
* Used to choose from key combinations.
* @param keyCombinations Array objects identifier-key names.
* @param jsonArrays Any amount of JSON-arrays to merge.
*
* @return Merged array.
*/
public static JsonArray mergeArrays(
@NonNull
String arrayName,
@NonNull
HashMap<String, String[]> keyCombinations,
Object... jsonArrays)
JsonArray resultArray = new JsonArray();
for (Object jsonArray : jsonArrays)
JsonArray array = (JsonArray) jsonArray;
for (JsonElement item : array)
if (
item.isJsonObject() &&
keyCombinations.get(arrayName) != null &&
keyCombinations.get(arrayName).length > 0)
// Array element is an object with identifier-keys:
ArrayList<JsonElement> resultArrayObjectsFound =
getArrayObjectsByKeyValues(
resultArray,
item.getAsJsonObject(),
keyCombinations.get(arrayName));
if (resultArrayObjectsFound.size() > 0)
// Such field is already present, merge is required:
JsonObject resultArrayObjectFound =
resultArrayObjectsFound.get(0).getAsJsonObject();
JsonObject mergedObject =
mergeObjects(
keyCombinations,
resultArrayObjectFound,
item.getAsJsonObject());
resultArray.remove(resultArrayObjectFound);
resultArray.add(mergedObject);
continue;
if (!resultArray.contains(item))
// No such element - add:
resultArray.add(item);
else if (
count(resultArray, item) < count(array, item))
// There are more duplicates of the element - add:
resultArray.add(item);
return resultArray;
/**
* Convert String to JSON-Array (GSON).
*
* @param jsonString JSON-array as string.
*
* @return JSON-array as GSON-array.
*/
public static JsonArray getJsonArray(String jsonString)
JsonArray jsonArray = new JsonArray();
JsonParser parser;
parser = new JsonParser();
try
jsonArray =
parser
.parse(
jsonString)
.getAsJsonArray();
catch (Exception ignore)
return jsonArray;
/**
* Find array objects that have required identity keys and match the values.
*
* @param array Array to search in.
* @param object Example object for search.
* Contains required keys and values.
* @param keys Object identity keys.
*
* @return Matching JSON-elements.
*/
public static ArrayList<JsonElement> getArrayObjectsByKeyValues(
JsonArray array,
JsonObject object,
String[] keys)
ArrayList<JsonElement> elements = new ArrayList<>();
for (JsonElement arrayElement : array)
if (arrayElement.isJsonObject())
JsonObject jsonObject = arrayElement.getAsJsonObject();
boolean hasAllKeysThatMatch = true;
for (String key : keys)
if (!jsonObject.has(key))
// One of the keys is not found:
hasAllKeysThatMatch = false;
break;
else
if (
jsonObject.get(key).isJsonPrimitive() &&
!jsonObject.get(key).equals(object.get(key)))
// Primitive type key values don't match:
hasAllKeysThatMatch = false;
break;
if ((
jsonObject.get(key).isJsonObject() ||
jsonObject.get(key).isJsonArray()) &&
!jsonObject.get(key).toString().equals(
object.get(key).toString()))
// Complex type key values don't match:
hasAllKeysThatMatch = false;
break;
if (hasAllKeysThatMatch)
// Key values match:
elements.add(jsonObject);
return elements;
/**
* Count given elements in array.
*
* @param element Element to find.
*
* @return Amount of given elements in array.
*/
public static int count(
JsonArray array,
JsonElement element)
int count = 0;
for (JsonElement currentElement : array)
if (currentElement.isJsonPrimitive())
// Primitive type:
if (currentElement.equals(element))
count++;
if (
currentElement.isJsonObject() ||
currentElement.isJsonArray())
// Complex type:
if (currentElement.toString().equals(element.toString()))
count++;
return count;
【讨论】:
以上是关于在 Java 中使用 Gson 合并/扩展 JSON 对象的主要内容,如果未能解决你的问题,请参考以下文章