在jackson json(Java)中以平面结构序列化List内容 [英] Serialize List content in a flat structure in jackson json (Java)

查看:351
本文介绍了在jackson json(Java)中以平面结构序列化List内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要序列化的类:

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

    @JsonProperty("CustomerId")
    private String customerId;

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

    //Getters and setters
}

我的产品详细信息。 java class:

My ProductDetails.java class:

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

换句话说,我试图跳过产品列表的JSON括号,并为每个ProductId和具有递增整数的ProductName字段。

In other words, I am trying to skip the JSON brackets for the Products-list and suffix each of the ProductId and ProductName fields with a increasing integer.

非常感谢任何帮助!

推荐答案

作为@Boris,蜘蛛指出。编写自定义序列化程序是解决方案。不像我预期的那样痛苦: - )

As @Boris the spider pointed out. Writing a custom serializer was the solution. Not as painful as I expected :-)

这篇关于在jackson json(Java)中以平面结构序列化List内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆