Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 757
Stupidfrog Avatar asked Apr 10 '14 03:04

Stupidfrog


People also ask

What is the difference between JsonNode and ObjectNode?

JsonNode represents any valid Json structure whereas ObjectNode and ArrayNode are particular implementations for objects (aka maps) and arrays, respectively.

How do you pass a list in ObjectNode?

You can use the putArray method which creates an ArrayNode . Then you should fill it with the elements from your list. addAll expects an ArrayNode or a liste of JsonObjects, you'll want to iterate through your list to add its strings to the new ArrayNode. @EricMaziade, you are right.

What is JsonNode Jackson?

JsonNode is Jackson's tree model (object graph model) for JSON. Jackson can read JSON into a JsonNode instance, and write a JsonNode out to JSON. This Jackson JsonNode tutorial will explain how to deserialize JSON into a JsonNode and serialize a JsonNode to JSON.


4 Answers

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);
like image 172
hsluo Avatar answered Oct 16 '22 20:10

hsluo


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);
like image 26
Sotirios Delimanolis Avatar answered Oct 16 '22 19:10

Sotirios Delimanolis


List<String> yourList = new ArrayList<>();
((ObjectNode) someNode).set("listname", this.objectMapper.valueToTree(yourList));
like image 4
swarnim gupta Avatar answered Oct 16 '22 20:10

swarnim gupta


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);
like image 1
transang Avatar answered Oct 16 '22 20:10

transang