【发布时间】:2022-01-25 11:52:44
【问题描述】:
我正在使用 Jackson 库创建 JSON。
例如,我有一个员工,其中包含以下字段:姓名、姓氏和一对信息列表,如下所示
@Data
@NoArgsConstructors
public class EmployeeJSON {
private String name;
private String surname;
private List<CoupleInfo> addresses = new ArrayList<>();
// this class continue with regular implementation to fill the fields and creating an object for CoupleInfo
为 CoupleInfo 实现了一个类:
@Data
@NoArgsConstructors
public class CoupleInfo {
private List<String> list = new ArrayList<>();
public CoupleInfo(String place, String address){
list.add(place);
list.add(address);
}
}
我想进入 JSON,当然包含字段名称和姓氏,字段地址填充为一对值的列表(作为 JSON 中的数组),但在数组内部没有字段列表.
怎么做?
我尝试使用@JsonIgnore,但所有字段和值都消失了。
最后得到这个:
{"name":"giulio", "surname":"marri", "addresses":[["roma","piazza liberta'"],["torino","via torre"]]}
改为:
{"name":"giulio", "surname":"marri", "addresses":[{"list":["roma","piazza liberta'"]},{"list":["torino","via torre"]}}
【问题讨论】: