问题
I have the below ObjectNode.
handlerObjectNode -> {"Info":{"Brand":{"BrandName":"TOP OF THE WORLD"}}}
I have another ObjectNode in the following format.
fieldObjects -> {"Description":"REGULAR BR"}
How can I create the below ObjectNode from the above two?
{
"Info": {
"Brand": {
"BrandName": "TOP OF THE WORLD"
}
"Description": "REGULAR BR"
}
}
I tried the below code.
handlerObjectNode.setAll(fieldObjects);
But it results in the following ObjectNode.
{
"Info": {
"Brand": {
"BrandName": "TOP OF THE WORLD"
}
},
"Description": "REGULAR BR"
}
I am using the com.fasterxml.jackson.databind.node.ObjectNode from Jackson. Any help would be much appreciated.
回答1:
Try This,
root.with("Info").put("Description", "REGULAR BR");
For more info This
回答2:
Extract the Info
object and add the fieldObjects
to that:
ObjectMapper om = new ObjectMapper();
ObjectNode handlerObjectNode = (ObjectNode) om.readTree("{\"Info\":{\"Brand\":{\"BrandName\":\"TOP OF THE WORLD\"}}}");
ObjectNode fieldObjects = (ObjectNode) om.readTree("{\"Description\":\"REGULAR BR\"}");
ObjectNode info = (ObjectNode) handlerObjectNode.path("Info");
info.setAll(fieldObjects);
Results in the following output:
{
"Info" : {
"Brand" : {
"BrandName" : "TOP OF THE WORLD"
},
"Description" : "REGULAR BR"
}
}
来源:https://stackoverflow.com/questions/41712865/how-to-add-an-objectnode-into-another-objectnode-as-a-child-using-jackson