The Jackson library implements the merging of JSON data. It can adopt deep merging (recursive strategy) based on tree model, which can retain all levels of data of two JSONs to avoid overwrite conflicts. In actual development, Jackson is feature-rich, safe and reliable, and has good compatibility, which allows program developers to easily convert JavaBean and JSON data saved in the "key:value" structure.
1. Create Jackson tool class
Create the JacksonUtil class (Jackson tool class) to implement the merge method of JSON data.
package ; import ; import ; import ; /** * Jackson Tools * @author pan_junbiao **/ public class JacksonUtil { private static ObjectMapper mapper = new ObjectMapper(); /** * Merge JSON data * @return Merged JsonNode object */ public static JsonNode mergeJsonNode(JsonNode mainNode, JsonNode updateNode) { //Parameter verification if (mainNode == null && updateNode == null) { return null; } if (mainNode == null) { return updateNode; } if (updateNode == null) { return mainNode; } if (() && ()) { ObjectNode mergedNode = (ObjectNode) mainNode; ().forEachRemaining(entry -> { String key = (); JsonNode value = (); if ((key)) { (key, mergeJsonNode((key), value)); } else { (key, value); } }); return mergedNode; } else { // Direct overwrite of non-object types (such as arrays, basic types) return updateNode; } } /** * Merge JSON data * @return Merged JSON string */ public static String mergeJsonString(String mainJson, String updateJson) { //Parameter verification if ((mainJson == null || () == 0) && (updateJson == null || () == 0)) { return null; } if (mainJson == null || () == 0) { return updateJson; } if (updateJson == null || () == 0) { return mainJson; } try { //Merge JSON data JsonNode mainNode = (mainJson); JsonNode updateNode = (updateJson); JsonNode resultNode = mergeJsonNode(mainNode, updateNode); return (resultNode); } catch (Exception e) { (); } return null; } }
2. Test the merge of JSON data
Create a test method to test the merge result of JSON data.
/** * Merge JSON data */ @Test public void mergeJsonTest() { try { String mainNode = "{\n" + " \"userId\": 1,\n" + " \"userName\": \"pan_junbiao's blog\"\n" + "}"; String updateNode = "{\n" + " \"userName\": \"pan_junbiao's blog_001\",\n" + " \"blogName\": \"Hello, welcome to pan_junbiao's blog\",\n" + " \"blogUrl\": \"/pan_junbiao\",\n" + " \"hobbies\": [\"swim\", \"basketball\", \"football\"]\n" + "}"; //Merge JSON data String result = (mainNode, updateNode); //Print results (result); } catch (Exception ex) { (); } }
Execution results:
{
"userId": 1,
"userName": "pan_junbiao's blog_001",
"blogName": "Hello, welcome to pan_junbiao's blog",
"blogUrl": "/pan_junbiao",
"hobbies": ["Swimming", "Basketball", "Football"]
}
This is the end of this article about using Jackson to realize the merger of JSON data. For more related Jackson JSON data merging content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!