java 如何在java中将两个json字符串合并为一个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35747813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to merge two json Strings into one in java
提问by Prashant Thorat
If we have given 2 Strings of type json, how can we merge them into single json String in java?
如果我们给出了 2 个 json 类型的字符串,我们如何在 java 中将它们合并为单个 json 字符串?
e.g.
String json1 = {
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S"
}
}
}
String json2 = {
"glossary": {
"title": "person name",
"age": "25"
}
}
Should produce
应产生
String mergedJson = {
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S"
},
"age": "25"
}
}
采纳答案by Prashant Thorat
Here is the code which recursively merges two jsons. This outputs as excepted:
这是递归合并两个json的代码。此输出除外:
private static JsonObject merge(JsonObject json1Obj, JsonObject json2Obj) {
Set<Entry<String, JsonElement>> entrySet1 = json1Obj.entrySet();
for (Entry<String, JsonElement> entry : entrySet1) {
String key1 = entry.getKey();
if (json2Obj.get(key1) != null) {
JsonElement tempEle2 = json2Obj.get(key1);
JsonElement tempEle1 = entry.getValue();
if (tempEle2.isJsonObject() && tempEle1.isJsonObject()) {
JsonObject mergedObj = merge(tempEle1.getAsJsonObject(),
tempEle2.getAsJsonObject());
entry.setValue(mergedObj);
}
}
}
Set<Entry<String, JsonElement>> entrySet2 = json2Obj.entrySet();
for (Entry<String, JsonElement> entry : entrySet2) {
String key2 = entry.getKey();
if (json1Obj.get(key2) == null) {
json1Obj.add(key2, entry.getValue());
}
}
return json1Obj;
}
回答by Darshan Mehta
Below code should do it, with a couple of assumptions:
下面的代码应该这样做,有几个假设:
- You are using ObjectMapper of Hymanson library (
com.fasterxml.Hymanson.databind.ObjectMapper
) to serialise/deserialise json fields of json1 will always overwrite json2 while merging
ObjectMapper mapper = new ObjectMapper(); Map<String, Object> map1 = mapper.readValue("json1", Map.class); Map<String, Object> map2 = mapper.readValue("json2", Map.class); Map<String, Object> merged = new HashMap<String, Object>(map2); merged.putAll(map1); System.out.println(mapper.writeValueAsString(merged));
- 您正在使用 Hymanson 库 (
com.fasterxml.Hymanson.databind.ObjectMapper
) 的ObjectMapper来序列化/反序列化 json 合并时 json1 的字段将始终覆盖 json2
ObjectMapper mapper = new ObjectMapper(); Map<String, Object> map1 = mapper.readValue("json1", Map.class); Map<String, Object> map2 = mapper.readValue("json2", Map.class); Map<String, Object> merged = new HashMap<String, Object>(map2); merged.putAll(map1); System.out.println(mapper.writeValueAsString(merged));
回答by T8xi
So I'm quite late to the party but I wanted to share my solution if anybody stumbles across this.
You can deeply merge two json strings with com.fasterxml.Hymanson.core:Hymanson-databind
ObjectMapper.readerForUpdating()
.
所以我参加聚会已经很晚了,但如果有人偶然发现这个问题,我想分享我的解决方案。您可以将两个 json 字符串与com.fasterxml.Hymanson.core:Hymanson-databind
ObjectMapper.readerForUpdating()
.
In this scenario you pass in two Json as String and merge them via readerForUpdating (untested code):
在这种情况下,您将两个 Json 作为字符串传递并通过 readerForUpdating(未经测试的代码)合并它们:
public String mergeJsonStrings(String json1, String json2) {
ObjectMapper mapper = new ObjectMapper();
ObjectReader reader = mapper.readerForUpdating(json1);
String result = reader.readValue(json2);
return result;
}
I used similar code to merge a property into an existing dataset. In this example the SomeProperties
class contains a hashmap which holds the properties for a specific user. The passed in propertiesString
is a single dot separated property e.g. some.random.property=value
. The property will be transformed into a JsonNode
with com.fasterxml.Hymanson.dataformat:Hymanson-dataformat-properties
.
我使用类似的代码将属性合并到现有数据集中。在此示例中,SomeProperties
该类包含一个哈希图,其中包含特定用户的属性。传入的propertiesString
是一个单点分隔的属性,例如some.random.property=value
. 该属性将转换为JsonNode
with com.fasterxml.Hymanson.dataformat:Hymanson-dataformat-properties
。
public SomeProperties mergeProperties(SomeProperties someProperties, String propertiesString) {
JavaPropsMapper javaPropsMapper = new JavaPropsMapper();
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = javaPropsMapper.readTree(propertiesString);
ObjectReader objectReader = mapper.readerForUpdating(someProperties.getProperties());
HashMap<String, Object> mergedProperties = objectReader.readValue(jsonNode);
someProperties.setProperties(mergedProperties);
return someProperties;
}
In both cases everything passed into objectReader.readValue()
will override existing keys.
在这两种情况下,传入的所有内容都objectReader.readValue()
将覆盖现有的键。