Java递归填充一个对象

huangapple go评论72阅读模式
英文:

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&lt;Employee&gt; mapEmployeeModel(List&lt;Employee&gt; employeeList, List&lt;Employee&gt; 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;
}

huangapple
  • 本文由 发表于 2020年8月16日 12:01:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63433018.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定