Jackson Json: how to convert array to JsonNode and ObjectNode?
You can just create a POJO that contains a List<Employee>
class Employees {
@JsonProperty("Employee")
private List<Employee> employees;
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
}
and serialize it to an ObjectNode
.
Employees e = new Employees();
List<Employee> employees = new ArrayList<Employee>();
e.setEmployees(employees);
ObjectNode objectNode = mapper.valueToTree(e);
List<String> yourList = new ArrayList<>();
((ObjectNode) someNode).set("listname", this.objectMapper.valueToTree(yourList));
ObjectMapper mapper = new ObjectMapper();
List<Employee> e = new ArrayList<Employee>();
ArrayNode array = mapper.valueToTree(e);
ObjectNode companyNode = mapper.valueToTree(company);
companyNode.putArray("Employee").addAll(array);
JsonNode result = mapper.createObjectNode().set("company", companyNode);