我必须使用 beanIO 将对象列表写入 json 文件。每当我尝试时,我只会得到第一个被写入文件的对象,如下所示。
{"employeeDetails":[{"recordType":"I","empId":"100","empName":"Name1"}]}
实际结果应该如下:
{"employeeDetails":[{"recordType":"I","empId":"100","empName":"Name1"},{"recordType":"I","empId":"101","empName":"Name2"}]}
使用 pojo 如下:
@Record
public class Employee{
@Field(minOccurs=0)
private String recordType;
@Field(minOccurs=0)
private String empId;
@Field(minOccurs=0)
private String empName;
// getters and setters
}
@Record
public class Department{
@Segment(minOccurs=0, collection=List.class)
private List<Employee> employeeDetails;
//getters and setters
}
这就是我的 impl 类所做的,
StreamFactory streamFactory=StreamFactory.newInstance();
streamFactory.loadResource(beanIoPath + beanIoMappingFileName);
Writer outJson = new BufferedWriter(new FileWriter(new File(absPath+fileName)));
BeanWriter jsonBeanWriter = streamFactory.createWriter(mapper, outJson);
Department dpt = //fetch from db;
jsonBeanWriter.write(dpt);
请建议应该添加什么,如何使用 BeanIO 实现将对象列表写入 json 文件。
谢谢..