JSON by Jackson:隐藏字段名称但保留值

JSON by Jackson: hide field name but keep the value

我正在使用 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 实现了 class:

@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"]}}

您可以使用 @JsonValue 将地址作为没有字段名称的列表列表获取:

@Data
@NoArgsConstructor
public class CoupleInfo {

    public List<String> list = new ArrayList<>();

    public CoupleInfo(String place, String address){
        list.add(place);
        list.add(address);
    }

    @JsonValue
    public List<String> getList() {
        return list;
    }
}

结果将是:

{"name":"giulio","surname":"marri","addresses":[["roma","piazza liberta"],["torino","via torre"]]}