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.
JsonNode represents any valid Json structure whereas ObjectNode and ArrayNode are particular implementations for objects (aka maps) and arrays, respectively.
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.
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.
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);
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));
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With