如何使用 jackson 为单个密钥的多个节点创建 JSON
How to create JSON for multiple nodes for a single key with jackson
我想在 Java 中使用 Jackson 创建一个像下面这样的 JSON。我能够做到这一点,但只有一把钥匙。在这种情况下,“TestProject1-staging”无法同时保存“children”和“vars”。
"TestProject1-staging": {
"children" : [ "TestProject1-staginga", "TestProject1-stagingb", "TestProject1-stagingc" ],
"vars": {
"projects": {
"TestProject1": {
"app_tier": "apptier",
"remote_dir": "/release/Test1"
}
}
}
}
这是我写的代码:
ObjectMapper mapper = new ObjectMapper();
ObjectNode projects = mapper.createObjectNode();
ObjectNode finalObj = mapper.createObjectNode();
ObjectNode proRootNode = mapper.createObjectNode();
ObjectNode children = mapper.createObjectNode();
ObjectNode proRoot = mapper.createObjectNode();
proRoot.put("app_tier", inventoryContainer.appTier);
proRoot.put("remote_dir", inventoryContainer.remoteDirectory);
proRootNode.set(projectRoot, proRoot);
projects.set("projects", proRootNode);
children.put("children",
mapper.writerWithDefaultPrettyPrinter().writeValueAsString(inventoryContainer.groups));
stagingVarNotSet = false;
finalObj.set(inventoryContainer.projectName, children);
varNode.set("vars", projects);
//finalObj.set(inventoryContainer.projectName, varNode);
System.out.println(StringEscapeUtils.unescapeJava(
mapper.writerWithDefaultPrettyPrinter().writeValueAsString(finalObj)));
如您所见,注释行试图设置 vars
变量。如果我取消注释,将打印 vars
但 children
将丢失。在当前格式中,它打印 children
但不打印 vars
.
那么我怎样才能将它们同时打印出来呢?
检查这是否适合您。
JSonCreator.java
package json;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
public class JSonCreator {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.createObjectNode();
// Create TestProject1 JSON Object
ObjectNode testProject1Node = mapper.createObjectNode();
testProject1Node.put("app_tier", "apptier");
testProject1Node.put("remote_dir", "/release/Test1");
// Create projects JSON Object
ObjectNode projectsNode = mapper.createObjectNode();
projectsNode.set("TestProject1", testProject1Node);
// Create vars JSON Object
ObjectNode varsNode = mapper.createObjectNode();
varsNode.set("projects", projectsNode);
// Create children JSON Array
ArrayNode childrenArrayNode = mapper.createArrayNode();
childrenArrayNode.add("TestProject1-staginga");
childrenArrayNode.add("TestProject1-stagingb");
childrenArrayNode.add("TestProject1-stagingc");
// Create children JSON object
ObjectNode childrenNode = mapper.createObjectNode();
childrenNode.set("children", childrenArrayNode);
// Create TestProject1-staging" JSON object
ObjectNode testProject1stagingNode = mapper.createObjectNode();
testProject1stagingNode.set("children", childrenArrayNode);
testProject1stagingNode.set("vars", varsNode);
// append into root node
((ObjectNode) rootNode).set("TestProject1-staging",
testProject1stagingNode);
// convert ObjectNode to pretty-print JSON
String json = null;
try {
json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(
rootNode);
// print json
System.out.println(json);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
输出:
{
"TestProject1-staging" : {
"children" : [ "TestProject1-staginga", "TestProject1-stagingb", "TestProject1-stagingc" ],
"vars" : {
"projects" : {
"TestProject1" : {
"app_tier" : "apptier",
"remote_dir" : "/release/Test1"
}
}
}
}
}
我想在 Java 中使用 Jackson 创建一个像下面这样的 JSON。我能够做到这一点,但只有一把钥匙。在这种情况下,“TestProject1-staging”无法同时保存“children”和“vars”。
"TestProject1-staging": {
"children" : [ "TestProject1-staginga", "TestProject1-stagingb", "TestProject1-stagingc" ],
"vars": {
"projects": {
"TestProject1": {
"app_tier": "apptier",
"remote_dir": "/release/Test1"
}
}
}
}
这是我写的代码:
ObjectMapper mapper = new ObjectMapper();
ObjectNode projects = mapper.createObjectNode();
ObjectNode finalObj = mapper.createObjectNode();
ObjectNode proRootNode = mapper.createObjectNode();
ObjectNode children = mapper.createObjectNode();
ObjectNode proRoot = mapper.createObjectNode();
proRoot.put("app_tier", inventoryContainer.appTier);
proRoot.put("remote_dir", inventoryContainer.remoteDirectory);
proRootNode.set(projectRoot, proRoot);
projects.set("projects", proRootNode);
children.put("children",
mapper.writerWithDefaultPrettyPrinter().writeValueAsString(inventoryContainer.groups));
stagingVarNotSet = false;
finalObj.set(inventoryContainer.projectName, children);
varNode.set("vars", projects);
//finalObj.set(inventoryContainer.projectName, varNode);
System.out.println(StringEscapeUtils.unescapeJava(
mapper.writerWithDefaultPrettyPrinter().writeValueAsString(finalObj)));
如您所见,注释行试图设置 vars
变量。如果我取消注释,将打印 vars
但 children
将丢失。在当前格式中,它打印 children
但不打印 vars
.
那么我怎样才能将它们同时打印出来呢?
检查这是否适合您。
JSonCreator.java
package json;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
public class JSonCreator {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.createObjectNode();
// Create TestProject1 JSON Object
ObjectNode testProject1Node = mapper.createObjectNode();
testProject1Node.put("app_tier", "apptier");
testProject1Node.put("remote_dir", "/release/Test1");
// Create projects JSON Object
ObjectNode projectsNode = mapper.createObjectNode();
projectsNode.set("TestProject1", testProject1Node);
// Create vars JSON Object
ObjectNode varsNode = mapper.createObjectNode();
varsNode.set("projects", projectsNode);
// Create children JSON Array
ArrayNode childrenArrayNode = mapper.createArrayNode();
childrenArrayNode.add("TestProject1-staginga");
childrenArrayNode.add("TestProject1-stagingb");
childrenArrayNode.add("TestProject1-stagingc");
// Create children JSON object
ObjectNode childrenNode = mapper.createObjectNode();
childrenNode.set("children", childrenArrayNode);
// Create TestProject1-staging" JSON object
ObjectNode testProject1stagingNode = mapper.createObjectNode();
testProject1stagingNode.set("children", childrenArrayNode);
testProject1stagingNode.set("vars", varsNode);
// append into root node
((ObjectNode) rootNode).set("TestProject1-staging",
testProject1stagingNode);
// convert ObjectNode to pretty-print JSON
String json = null;
try {
json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(
rootNode);
// print json
System.out.println(json);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
输出:
{
"TestProject1-staging" : {
"children" : [ "TestProject1-staginga", "TestProject1-stagingb", "TestProject1-stagingc" ],
"vars" : {
"projects" : {
"TestProject1" : {
"app_tier" : "apptier",
"remote_dir" : "/release/Test1"
}
}
}
}
}