英文:
How to access Corresponding Inner List Item while using Streams
问题
这是我给您翻译好的内容:
public static void main(String[] args) {
    String json = "您的 JSON 数据"; // 这里是您的 JSON 数据,由于长度较长,我无法直接显示,您可以将原始 JSON 数据放在这里。
    Gson gson = new Gson();
    Root empResp = gson.fromJson(json, Root.class);
    
    Optional.ofNullable(empResp)
            .map(Root::getEmployees)
            .map(Employees::getEmp)
            .orElseGet(Collections::emptyList)
            .stream()
            .forEach(emp -> {
                int empId = emp.getEmpidid();
                String empName = emp.getEmpname();
                List<Dept> departments = emp.getDepartments().getDept();
                
                for (Dept dept : departments) {
                    String deptCode = dept.getCode();
                    // 在这里使用 empId, empName, deptCode 做相应处理
                }
            });
}
请注意,由于您的 JSON 数据较长,我无法在单个回答中显示完整的 JSON 数据和完整的类结构,因此我在上述代码中将 JSON 数据的部分用 "您的 JSON 数据" 来代替。请确保您将实际的 JSON 数据和类结构替换到相应的位置。
此代码演示了如何在循环 emp 列表时访问相应的部门代码(dept code)。在循环 emp 列表的过程中,我们首先获取每个 emp 对象的 ID、姓名和部门列表,然后再在部门列表中循环以获取部门代码。您可以在标有注释的部分执行您需要的操作。
英文:
I have the following JSON Structure
{
	"employees": {
		"emp": [{
				"departments": {
					"dept": [{
						"code": "S",
						"description": "FieldWork"
					}]
				},
				"empidid": 35,
				"empname": "Mark"
			}
		]
	}
}
Each emp is a List which has corresponding dept List
While looping emp as a List , i need corresponding Dept Code also
I am facing trouble getting the corresponding dept code while looping
public static void main(String[] args) {
		
		
		String json = "{\r\n" + 
				"	\"employees\": {\r\n" + 
				"		\"emp\": [{\r\n" + 
				"				\"departments\": {\r\n" + 
				"					\"dept\": [{\r\n" + 
				"						\"code\": \"S\",\r\n" + 
				"						\"description\": \"FieldWork\"\r\n" + 
				"					}]\r\n" + 
				"				},\r\n" + 
				"				\"empidid\": 35,\r\n" + 
				"				\"empname\": \"Mark\"\r\n" + 
				"			}\r\n" + 
				"\r\n" + 
				"		]\r\n" + 
				"	}\r\n" + 
				"}";
		
		Gson gson = new Gson();
		RootVal empResp = gson.fromJson(json, RootVal.class);
		
		Optional.ofNullable(empResp)
		.map(e -> e.getEmployees())
		.map(e -> e.getEmp())
		
		.orElseGet(Collections::emptyList).stream().map(emp -> {
			
			emp.getEmpidid();
			emp.getEmpidid();
			// How to get DEPT Code Here 
			return null;
		}).collect(Collectors.toList());
		
		
	}
This is my class Structure
==================================
public class Dept
{
    private String code;
    private String description;
    public void setCode(String code){
        this.code = code;
    }
    public String getCode(){
        return this.code;
    }
    public void setDescription(String description){
        this.description = description;
    }
    public String getDescription(){
        return this.description;
    }
}
==================================
import java.util.ArrayList;
import java.util.List;
public class Departments
{
    private List<Dept> dept;
    public void setDept(List<Dept> dept){
        this.dept = dept;
    }
    public List<Dept> getDept(){
        return this.dept;
    }
}
==================================
public class Emp
{
    private Departments departments;
    private int empidid;
    private String empname;
    public void setDepartments(Departments departments){
        this.departments = departments;
    }
    public Departments getDepartments(){
        return this.departments;
    }
    public void setEmpidid(int empidid){
        this.empidid = empidid;
    }
    public int getEmpidid(){
        return this.empidid;
    }
    public void setEmpname(String empname){
        this.empname = empname;
    }
    public String getEmpname(){
        return this.empname;
    }
}
==================================
import java.util.ArrayList;
import java.util.List;
public class Employees
{
    private List<Emp> emp;
    public void setEmp(List<Emp> emp){
        this.emp = emp;
    }
    public List<Emp> getEmp(){
        return this.emp;
    }
}
==================================
public class Root
{
    private Employees employees;
    public void setEmployees(Employees employees){
        this.employees = employees;
    }
    public Employees getEmployees(){
        return this.employees;
    }
}
Please let me how to access Corresponding Dept Code while looping emp List
答案1
得分: 2
你可以使用 Emp 类的字段获取方法,类似这样:
emp -> emp.getDepartments().getDept()
在这里,getDept() 返回 List<Dept>,所以要获取 code,你可以对列表中的每个 Dept 对象使用获取方法,例如 .get(0).getCode(),以获取 Dept 列表中第一个元素的 code。
完整代码:
Optional.ofNullable(empResp)
    .map(e -> e.getEmployees())
    .map(e -> e.getEmp())
        
    .orElseGet(Collections::emptyList).stream().map(emp -> {
        emp.getEmpidid();
        emp.getEmpidid();
        // 在这里如何获取 DEPT Code
        emp.getDepartments().getDept().get(0).getCode();
        return null;
    }).collect(Collectors.toList());
英文:
You can use the field's getter method of Emp class and so on like
emp -> emp.getDepartments().getDept()
Here, getDept() return List<Dept> so to get code, you can use getter for each Dept object of list. Like .get(0).getCode() to get first element's code of Dept list.
Full code:
Optional.ofNullable(empResp)
    .map(e -> e.getEmployees())
    .map(e -> e.getEmp())
    
    .orElseGet(Collections::emptyList).stream().map(emp -> {
        emp.getEmpidid();
        emp.getEmpidid();
        // How to get DEPT Code Here 
        emp.getDepartments().getDept().get(0).getCode();
        return null;
    }).collect(Collectors.toList());
专注分享java语言的经验与见解,让所有开发者获益!



评论