英文:
Java recursively populate a Object
问题
这里有一个 JSON,是一个对象列表,其中每个对象都有一个自己类型的属性(属性 manager 的类型是 employee),如下所示:
{
"employee": [
{
"id": "",
"name": "",
"address": {
"city": "",
"state": ""
},
"manager": {
"id": "",
"name": "",
"address": {
"city": "",
"state": ""
},
"manager": null
}
}
]
}
我有以下属性的 DTO 类,其中 Employee 具有一个类型为 Employee 的成员变量。
class Employee {
private String id;
private Address address;
private Employee manager;
//getter & setter
}
class Address {
private String city;
//getter & setter
}
我的要求是递归地填充 Employee 对象(嵌套可以是 n 级 Employee -> Manager -> Manager -> Manager ...
),并形成一个新的 Employee 对象列表。
这将会得到以下结果:
{
"employee": [
{
"id": "",
"address": {
"city": ""
},
"manager": {
"id": "",
"address": {
"city": ""
},
"manager": null
}
}
]
}
任何建议都将是极大的帮助。
英文:
I have a json which is a list of objects with a property of its own type (property manager is of type employee) as below.
{
"employee": [
{
"id": "",
"name": "",
"address": {
"city": "",
"state": ""
},
"manager": {
"id": "",
"name": "",
"address": {
"city": "",
"state": ""
},
"manager": null
}
}
]
}
I have DTO classes with below properties where Employee has a member variable of type Employee.
class Employee{
private String id;
priavte Address address;
private Employee manager
//getter & setter
}
class Address{
private String city;
//getter & setter
}
My requirement is to populate Employee Object recursively (nesting can be of n level Employee -> Manager -> Manager -> Manager ...
) and form a new List of Employee Object.
Which will give us the result as below
{
"employee": [
{
"id": ""
"address": {
"city": ""
},
"manager": {
"id": ""
"address": {
"city": ""
},
"manager": null
}
}
]
}
Any suggestion would be a great help.
答案1
得分: 0
获得了解决上述问题的方法:
private List<Employee> mapEmployeeModel(List<Employee> employeeList, List<Employee> newEmployeeList) {
for (Employee emp : employeeList) {
Employee newEmp = mapEmployee(emp);
newEmployeeList.add(newEmp);
}
return newEmployeeList;
}
private Employee mapEmployee(Employee emp) {
Employee newEmp = new Employee();
newEmp.setId(emp.getId());
newEmp.setName(emp.getName());
newEmp.setAddress(mapAddress(emp));
if(emp.getManager() == null) {
return newEmp;
} else {
newEmp.setManager(mapEmployee(emp.getManager())); // 递归地设置经理
}
return newEmp;
}
private Address mapAddress(Employee emp) {
Address address = new Address();
address.setCity(emp.getAddress().getCity());
return address;
}
英文:
Got the way to achieve the solution of above problem
private List<Employee> mapEmployeeModel(List<Employee> employeeList, List<Employee> newEmployeeList) {
for (Employee emp : employeeList) {
Employee newEmp = mapEmployee(emp);
newEmployeeList.add(newEmp);
}
return newEmployeeList;
}
private Employee mapEmployee(Employee emp) {
Employee newEmp = new Employee ();
newEmp.setId(emp.getId());
newEmp.setName(emp.getName());
newEmp.setAddress(mapAddress(emp));
if(emp.getManager() == null) {
return newEmp;
} else {
newEmp.setManager(mapEmployee(emp.getManager()));// Recursively setting up the managers
}
return newEmp;
}
private Address mapAddress(Employee emp) {
Address address = new Address();
address.setCity(emp.getAddress().getCity);
return address;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论