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":
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.