Stupidfrog
Stupidfrog

Reputation: 2102

Jackson Json: how to convert array to JsonNode and ObjectNode?

Given Employee and company class

Company
{
    String companyName;
}

Employee
{
    String employeeName;
}

and my code like the following

List<Employee> e = new ArrayList<Employee>();
.....
.....

i wish i can get result like this

{
    "company":{ 
                "companyName":"cName",
                "Employee":[
                    {"employeeName":"myName1"},
                    {"employeeName":"myName2"},
                    {"employeeName":"myName3"}
                ]
              }
}

It is a simple question, however i quite confusing somethings.... Especially Gson And Json....

Please do not propose other library, which i COMPULSORY NEED this library to work. And due to some circumstance,this class IS NOT i wanted.

Company
{
    String companyName;
    List<Employee> employees;
}

Therefore i need MANUALLY put it,and serialize to json string.

EDITED: @Sotirios Delimanolis classes declared is the right way to design the relationship between classes. However, that's not i wanted.

The Answer from @hsluo is Correct! and same as what @Sotirios Delimanolis mention. Totally fulfill this question.

And i did found another way to do it which using Hashmap

HashMap k = new HashMap();
List<employee> y = new ArrayList<employee>();
y......
k.put("records", y);
k.put("total", total);

and then return to @Responbody, result totally same as @hsluo answered.

AND thanks @Sotirios Delimanolis, @hsluo to help me.

Upvotes: 38

Views: 115219

Answers (5)

swarnim gupta
swarnim gupta

Reputation: 231

List<String> yourList = new ArrayList<>();
((ObjectNode) someNode).set("listname", this.objectMapper.valueToTree(yourList));

Upvotes: 4

Daniel Alonso
Daniel Alonso

Reputation: 11

JsonNode result = new ObjectMapper().readTree(e.toString());

Upvotes: 1

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280000

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);

Upvotes: 1

Sang
Sang

Reputation: 4456

You can use jackson ObjectNode's put and putArray like

import com.fasterxml.jackson.databind.node.ObjectNode;
ArrayList<ObjectNode> employee = new ArrayList();
for(int i = 1; i < 4; i++){
  ObjectNode em = Json.newObject();
  em.put("companyName", "cName" + i);
  employee.add(em);
}
ObjectNode company = Json.newObject();
company.put("companyName", "cName");
company.putArray("employee").addAll(employee);
return Json.newObject().put("company", company);

Upvotes: 1

hsluo
hsluo

Reputation: 1702

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);

Upvotes: 86

Related Questions