0

我有一个看起来像这样的 json 对象

"price": {
        "sale": {
          "value": 39.99,
          "label": "Now"
        },
        "regular": {
          "value": 69.5,
          "label": "Orig."
        },
        "shippingfee": 0,
        "pricetype": "Markdown",
        "pricetypeid": 7,
        "onsale": true
}

“sale”和“regular”是关键字(唯一)是许多可用价格类型中的 2 个,标签“Orig”和“Now”是关键字(唯一)是许多可用标签中的 2 个。

我不确定将此价格 json 存储到 POJO 中的最佳数据结构。

有人可以指导我吗?

4

1 回答 1

1

我想您的问题是将销售和常规属性转换为统一的表示形式,该表示形式可能可以使用枚举作为唯一值。使用 JSON 序列化/反序列化的默认机制,这可能很困难。我不确定您正在使用的 JSON 解析库,在 Jackson 中,可以为字段注册自定义序列化器和反序列化器(使用注释)。在这种情况下,您要做的就是注册一个自定义序列化器/反序列化器并以您想要的方式处理 JSON 的字段。你可以参考这篇文章

针对评论添加了以下内容:

POJO 的可能结构如下:

publi class Price{

  protected List<PricePointDetails> pricePointDetails;
  protected float shippingFee;
  protected PriceTypes priceType;
  protected priceTypeId;
  protected boolean onSale;

}//class closing

public class PricePointDetails{

 protected PriceTypes priceType;
 protected float value;
 protected LabelTypes labelType;

}//class closing


public enumeration PriceTypes{

 sale,regular,markdown;

}//enumeration closing

public enumeration LabelTypes{

 Orig,Now;

}//enumeration closing

我添加的只是构建数据的一种方式,也可以通过其他方式完成。

于 2016-09-23T07:20:14.030 回答