Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
142 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try this:

ObjectMapper mapper = new ObjectMapper();
ObjectNode rootNode = mapper.createObjectNode();

ObjectNode childNode1 = mapper.createObjectNode();
childNode1.put("name1", "val1");
childNode1.put("name2", "val2");

rootNode.set("obj1", childNode1);

ObjectNode childNode2 = mapper.createObjectNode();
childNode2.put("name3", "val3");
childNode2.put("name4", "val4");

rootNode.set("obj2", childNode2);

ObjectNode childNode3 = mapper.createObjectNode();
childNode3.put("name5", "val5");
childNode3.put("name6", "val6");
    
rootNode.set("obj3", childNode3);


String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(rootNode);
System.out.println(jsonString);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...