我的以下设置工作正常,并且按预期打印了以下JSON:
{
"status" : {
"status" : "SUCCESS",
"message" : " "
},
"employeeStatus" : [ {
"ID" : 81,
"Name" : "Jack"
}, {
"ID" : 88,
"Name" : "Anthony"
} ]
}
以下代码可以正常工作,并且可以在JSON上方打印:
@RequestMapping(value="/get_employee_details", method=RequestMethod.GET)
public String updateemployee()
{
MyJSON myJSON = new MyJSON("SUCCESS", " ");
myJSON.addEmployeeStatus(81, "Jack");
myJSON.addEmployeeStatus(88, "Anthony");
return myJSON;
}
public class MyJSON {
private MyStatus status;
private List<EmployeeStatus> employeeStatus = new ArrayList<>();
public MyJSON(String status, String message) {
this.status = new MyStatus(status, message);
}
public void addEmployeeStatus(int id, String name) {
this.employeeStatus.add(new EmployeeStatus(id, name));
}
public MyStatus getStatus() {
return this.status;
}
public List<EmployeeStatus> getEmployeeStatus() {
return this.employeeStatus;
}
}
public class MyStatus {
private String status;
private String message;
public MyStatus(String status, String message) {
this.status = status;
this.message = message;
}
public String getStatus() {
return this.status;
}
public String getMessage() {
return this.message;
}
}
public class EmployeeStatus {
private int id;
private String name;
public EmployeeStatus(int id, String name) {
this.id = id;
this.name = name;
}
@JsonProperty("ID")
public int getId() {
return this.id;
}
@JsonProperty("Name")
public String getName() {
return this.name;
}
}
但是我还涉及其他一些事情,例如Hibernate 4.3.5,从那里我可以获得一些细节,因此我对上述代码进行了如下修改:
@RequestMapping(value="/get_employee_details", method=RequestMethod.GET)
public String updateemployee
(
@RequestParam(value="emp_id", defaultValue="0") Integer emp_id
)
{
EmployeeDao empDao = (EmployeeDao)context.getBean("empDao");
List<Employee> empList = empDao.findByEmId(emp_id);
MyJSON myJSON = new MyJSON("SUCCESS", " ");
myJSON.addEmployeeStatus(empList);
return myJSON;
}
public class MyJSON {
private MyStatus status;
private List<EmployeeStatus> employeeStatus = new ArrayList<>();
public MyJSON(String status, String message) {
this.status = new MyStatus(status, message);
}
public void addEmployeeStatus(List<Employee> empList) {
this.employeeStatus.add(new EmployeeStatus(empList));
}
public MyStatus getStatus() {
return this.status;
}
public List<EmployeeStatus> getEmployeeStatus() {
return this.employeeStatus;
}
}
public class EmployeeStatus {
private int id;
private String name;
private List<Employee> empList;
public EmployeeStatus(List<Employee> empList) {
this.empList = empList;
}
@JsonProperty("ID")
public int getId() {
return this.id;
}
@JsonProperty("Name")
public String getName() {
return this.name;
}
}
对于上述内容,我正在获取以下JSON响应:
{
"status": {
"status": "SUCCESS",
"message": " "
},
"employeeStatus": [
{
"ID": 0,
"Name": null
}
]
}
如果有人想知道empList
的输出是什么。从控制器的以下代码行中,我尝试像这样打印它:
List<Employee> empList = empDao.findByEmId(emp_id);
logger.info("Employee List Check " + empList);
它正像这样打印:
Employee List Check [abc.cde.myproject.orm.Employee@599eeded,abc.cde.myproject.orm.Employee@75ffa715,abc.cde.myproject.orm.Employee@152beb23 ]
有人可以告诉我我在上面做什么吗?我的意思是如何使用上述代码中的empList
打印JSON中的所有ID和名称?谢谢
答案 0 :(得分:0)
使用jakson,flexjson等任何库将Employee List<Employee> empList
的列表转换为JSON。
创建如下所示的方法。
public String toJsonArray(Collection<Employee> collection) {
return new JSONSerializer().transform(new DateTransformer("MM/dd/yyyy HH:mm:ss"), java.util.Date.class).include("ID","Name").exclude("*").serialize(empList);
}
注意:在pom / gradle文件中添加flexjson依赖项,以便一旦添加依赖项。导入将如下所示。
import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;
import flexjson.transformer.DateTransformer;