在jackson json(Java)中以平面结构序列化List内容

时间:2015-06-12 14:23:37

标签: java json jackson

我需要序列化的类:

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class MyClass {

    @JsonProperty("CustomerId")
    private String customerId;

    @JsonProperty("Products")
    private List<ProductDetails> products;

    //Getters and setters
}

我的ProductDetails.java类:

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class ProductDetails {
    @JsonProperty("ProductId")
    private String productId;

    @JsonProperty("ProductName")
    private String productName;

    //Getters and setters
}

默认的序列化输出:

{
  "CustomerId" : "ewm0po",

  "Products" : [ {
       "ProductId" : "AAA",
       "ProductName" : "AAA Product"
     },  {
       "ProductId" : "AAA",
       "ProductName" : "AAA Product"
   }]
}

我想要获得的输出:

{
  "CustomerId" : "ewm0po",
  "ProductId1" : "AAA",
  "ProductName1" : "AAA Product"
  "ProductId2" : "AAA",
  "ProductName2" : "AAA Product"
}

换句话说,我试图跳过Products-list的JSON括号,并以增加的整数为每个ProductId和ProductName字段添加后缀。

非常感谢任何帮助!

0 个答案:

没有答案