4

Here is my JSON, I want to convert into JAVA class

{
  "RELEASED": [
    {
      "purchaseOrderId": "2000",
      "date": "13/06/2019",
      "purchaseOrderLineId": "1",
      "itemCategory": "KuchItemCate1",
      "isDeliveryDelay": true,
      "isShipmentDelay": false
    }
  ],
  "SHIPPED": [
    {
      "purchaseOrderId": "1000",
      "date": "13/06/2019",
      "purchaseOrderLineId": "0",
      "itemCategory": "KuchItemCate0",
      "isDeliveryDelay": true,
      "isShipmentDelay": false
    },
    {
      "purchaseOrderId": "1000",
      "date": "13/06/2019",
      "purchaseOrderLineId": "2",
      "itemCategory": "KuchItemCate2",
      "isDeliveryDelay": true,
      "isShipmentDelay": false
    }
  ]
} 

java Root class:MTHVendorPerformance

public class MTHVendorPerformance implements Serializable {
    Map<String,List<MTHPOStatusDetails>> stringListMap;

    public Map<String, List<MTHPOStatusDetails>> getStringListMap() {
        return stringListMap;
    }

    public void setStringListMap(Map<String, List<MTHPOStatusDetails>> stringListMap) {
        this.stringListMap = stringListMap;
    }
    /*@JsonProperty("RELEASED")
    private List<MTHPOStatusDetails> released;
    @JsonProperty("SHIPPED")
    private List<MTHPOStatusDetails> shipped;

    public List<MTHPOStatusDetails> getShipped() {
        return shipped;
    }

    public void setShipped(List<MTHPOStatusDetails> shipped) {
        this.shipped = shipped;
    }

    public List<MTHPOStatusDetails> getReleased() {
        return released;
    }

    public void setReleased(List<MTHPOStatusDetails> released) {
        this.released = released;
    }*/
}

Java Inner class:MTHPOStatusDetails

public class MTHPOStatusDetails {
    private String purchaseOrderId;
    private String date;
    private String purchaseOrderLineId;
    private String itemCategory;
    private boolean isDeliveryDelay;
    private boolean isShipmentDelay;

    public String getPurchaseOrderId() {
        return purchaseOrderId;
    }

    public void setPurchaseOrderId(String purchaseOrderId) {
        this.purchaseOrderId = purchaseOrderId;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getPurchaseOrderLineId() {
        return purchaseOrderLineId;
    }

    public void setPurchaseOrderLineId(String purchaseOrderLineId) {
        this.purchaseOrderLineId = purchaseOrderLineId;
    }

    public String getItemCategory() {
        return itemCategory;
    }

    public void setItemCategory(String itemCategory) {
        this.itemCategory = itemCategory;
    }

    public boolean isDeliveryDelay() {
        return isDeliveryDelay;
    }

    public void setDeliveryDelay(boolean deliveryDelay) {
        isDeliveryDelay = deliveryDelay;
    }

    public boolean isShipmentDelay() {
        return isShipmentDelay;
    }

    public void setShipmentDelay(boolean shipmentDelay) {
        isShipmentDelay = shipmentDelay;
    }
}

Code for convert JSON to java object

code snippet:::::::::

ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
ClassLoader classLoader = getClass().getClassLoader();
JsonObject jsonObject = ConfigReader.getConfigFromClasspath("vendor_performance_mth_data.json");

mthVendorPerformance = mapper.readValue(jsonObject.toString(),MTHVendorPerformance.class);

After running above code I am getting below exception. So please let me know in case if you have this kind of issues

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of com.samsclub.fidal.data.model.vendorperformance.MTHPOStatusDetails out of START_ARRAY token
     at [Source: (String)"{"RELEASED":    
3
  • I am getting below error:: Commented Jun 14, 2019 at 14:06
  • 1
    Why is the "RELEASED" json property commented out? Commented Jun 14, 2019 at 14:09
  • Becoz that way I need to check each status in our code. I want something which is dynamic. just ignore that part. could you tell me what is the best way to convert below above mention json Commented Jun 17, 2019 at 13:13

1 Answer 1

2

Get and set prefixes are missing in getters and setters for isDeliveryDelay and isShipmentDelay, should be getIsDeliveryDelay() setIsDeliveryDelay(...) I think that you shadowing it by DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES.

If you want to have it stored in the map, you need to use: @JsonAnyGetter and @JsonAnySetter something like this:

public static class MTHVendorPerformance {
    private Map<String, List<MTHPOStatusDetails>> fields = new HashMap<>();

    @JsonAnyGetter
    public Map<String, List<MTHPOStatusDetails>> getFields() {
      return fields;
    }

    @JsonAnySetter
    public void setFields(String key, List<MTHPOStatusDetails> value) {
      fields.put(key, value);
    }
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.