user641887 user641887 - 1 year ago 117
JSON Question

Mongo JSON to Java POJO mapping

I have a json object which looks something like this

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

"sale" and "regular" are keywords (unique) are 2 of the many price-types available, and the labels
"Orig" and "Now" are keywords (unique) are 2 of the many labels available.

I am not sure the best data structure to store this price json into the POJO.

can someone please guide me through this ?

Answer Source

I guess your problem is to convert the sale and regular attributes into an uniform representation which probably could use an Enumeration for the unique values. With default mechanism of JSON serilization/deserialization, this could be difficult. I am not sure about the JSON parsing library you are using, in Jackson there is a possibility to register custom serializers and deserializers for fields (using annotations). In this case, whcy tou would do is to register a custom serializer/deserializer and handle the fields of the JSON in the way you want it. You could refer this post

Added the below in response to the comment:

A probable dtructure for the POJO could be as below:

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{


}//enumeration closing

public enumeration LabelTypes{


}//enumeration closing

What I have added, is just one way of structuring the data, it could be done in otherways also.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download