简体   繁体   中英

how to map json data which has dynamic fields like sku0,sku1 into a pojo class in java

below is my api response

{
    "skuInventory": {
        "sku0": {
            "inventory": {
                "PODate": {
                    "time": 1674363600000
                },
                "checkBackOrderQTY": false,
                "allocatedQuantity": 127,
                "endDate": {
                    "time": 1669216432575
                },
                "balanceQuantity": 4096,
                "ltr60Days": true,
                "modifiedDate": null,
                "availableQuantity": 0,
                "id": "VS-1261",
                "dateFirstReceived": {
                    "time": 1136178000000
                },
                "totalOnOrder": 4858,
                "remainDaysCurrDatePODate": 52,
                "pendingBackorders": 0,
                "presellFlag": false,
                "storeInventory": true
            },
            "quantityLimitWebPageMsg": "",
            "freeShippingPromoAmt": 25,
            "notCartableBrandOOSMsg": "",
            "cartableFlags": {
                "bopusOnlyMessage": "Item is unavailable for shipping, please check local stores for pickup availability",
                "ADP": "0",
                "BOPUS": "1",
                "consumeUpdateFlexShippingVerbiage": "true",
                "DTCEstShippingMessage": "Temporarily Out of Stock",
                "isProductFlexShippingFeeApplied": "false",
                "cartableSku": "1",
                "DTC": "0",
                "DTCAvailablityMessage": "Temporarily Out of Stock",
                "isProductFlexShippingFeeWaived": "false",
                "DTCEstShipMsgSiteExp": "Temporarily Out of Stock",
                "DTCAvMsgPDPRedesign": "Temporarily Out of Stock"
            },
            "quantityThreshold": 0
        }
    }
}

As we can see from above json structure there are multiple properties like sku0,sku1.sku2

I want to convert this json into POJO which i have created which is like below,

public class Root {
    
    private SkuInventory skuInventory;

    public SkuInventory getSkuInventory() {
        return skuInventory;
    }

    public void setSkuInventory(SkuInventory skuInventory) {
        this.skuInventory = skuInventory;
    }

}

public class SkuInventory {
    
    //@JsonAlias({ "sku0", "sku1", "sku2" })
    private List<Sku0> sku0;

    public List<Sku0> getSku0() {
        return sku0;
    }

    public void setSku0(List<Sku0> sku0) {
        this.sku0 = sku0;
    }

}

public class Sku {
    
    private Inventory inventory;
    
    private String quantityLimitWebPageMsg;
    
    private int freeShippingPromoAmt;
    
    private String notCartableBrandOOSMsg;
    
    private CartableFlags cartableFlags;
    
    private int quantityThreshold;

    public Inventory getInventory() {
        return inventory;
    }

    public void setInventory(Inventory inventory) {
        this.inventory = inventory;
    }

    public String getQuantityLimitWebPageMsg() {
        return quantityLimitWebPageMsg;
    }

    public void setQuantityLimitWebPageMsg(String quantityLimitWebPageMsg) {
        this.quantityLimitWebPageMsg = quantityLimitWebPageMsg;
    }

    public int getFreeShippingPromoAmt() {
        return freeShippingPromoAmt;
    }

    public void setFreeShippingPromoAmt(int freeShippingPromoAmt) {
        this.freeShippingPromoAmt = freeShippingPromoAmt;
    }

    public String getNotCartableBrandOOSMsg() {
        return notCartableBrandOOSMsg;
    }

    public void setNotCartableBrandOOSMsg(String notCartableBrandOOSMsg) {
        this.notCartableBrandOOSMsg = notCartableBrandOOSMsg;
    }

    public CartableFlags getCartableFlags() {
        return cartableFlags;
    }

    public void setCartableFlags(CartableFlags cartableFlags) {
        this.cartableFlags = cartableFlags;
    }

    public int getQuantityThreshold() {
        return quantityThreshold;
    }

    public void setQuantityThreshold(int quantityThreshold) {
        this.quantityThreshold = quantityThreshold;
    }
    
    

}

public class Inventory {
    
    @JsonProperty("PODate") 
    private Time pODate;
    
    private boolean checkBackOrderQTY;
    
    private int allocatedQuantity;
    
    private Time endDate;
    
    private int balanceQuantity;
    
    private boolean ltr60Days;
    
    private Object modifiedDate;
    
    private int availableQuantity;
    
    private String id;
    
    private Time dateFirstReceived;
    
    private int totalOnOrder;
    
    private int remainDaysCurrDatePODate;
    
    private int pendingBackorders;
    
    private boolean presellFlag;
    
    private boolean storeInventory;

    public Time getpODate() {
        return pODate;
    }

    public void setpODate(Time pODate) {
        this.pODate = pODate;
    }

    public boolean isCheckBackOrderQTY() {
        return checkBackOrderQTY;
    }

    public void setCheckBackOrderQTY(boolean checkBackOrderQTY) {
        this.checkBackOrderQTY = checkBackOrderQTY;
    }

    public int getAllocatedQuantity() {
        return allocatedQuantity;
    }

    public void setAllocatedQuantity(int allocatedQuantity) {
        this.allocatedQuantity = allocatedQuantity;
    }

    public Time getEndDate() {
        return endDate;
    }

    public void setEndDate(Time endDate) {
        this.endDate = endDate;
    }

    public int getBalanceQuantity() {
        return balanceQuantity;
    }

    public void setBalanceQuantity(int balanceQuantity) {
        this.balanceQuantity = balanceQuantity;
    }

    public boolean isLtr60Days() {
        return ltr60Days;
    }

    public void setLtr60Days(boolean ltr60Days) {
        this.ltr60Days = ltr60Days;
    }

    public Object getModifiedDate() {
        return modifiedDate;
    }

    public void setModifiedDate(Object modifiedDate) {
        this.modifiedDate = modifiedDate;
    }

    public int getAvailableQuantity() {
        return availableQuantity;
    }

    public void setAvailableQuantity(int availableQuantity) {
        this.availableQuantity = availableQuantity;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Time getDateFirstReceived() {
        return dateFirstReceived;
    }

    public void setDateFirstReceived(Time dateFirstReceived) {
        this.dateFirstReceived = dateFirstReceived;
    }

    public int getTotalOnOrder() {
        return totalOnOrder;
    }

    public void setTotalOnOrder(int totalOnOrder) {
        this.totalOnOrder = totalOnOrder;
    }

    public int getRemainDaysCurrDatePODate() {
        return remainDaysCurrDatePODate;
    }

    public void setRemainDaysCurrDatePODate(int remainDaysCurrDatePODate) {
        this.remainDaysCurrDatePODate = remainDaysCurrDatePODate;
    }

    public int getPendingBackorders() {
        return pendingBackorders;
    }

    public void setPendingBackorders(int pendingBackorders) {
        this.pendingBackorders = pendingBackorders;
    }

    public boolean isPresellFlag() {
        return presellFlag;
    }

    public void setPresellFlag(boolean presellFlag) {
        this.presellFlag = presellFlag;
    }

    public boolean isStoreInventory() {
        return storeInventory;
    }

    public void setStoreInventory(boolean storeInventory) {
        this.storeInventory = storeInventory;
    }

}

Below is the codee to map json into a POJO


ResponseEntity<?> inventoryData = (ResponseEntity<?>) inventoryAPIResponse.getData();
                Map<?, ?> inventoryBody = (Map<?, ?>) inventoryData.getBody();              
                Root atgInventory = getObjectMapper().convertValue(inventoryBody, Root.class);

                Sku availableQuantity = (Sku) atgInventory.getSkuInventory().getSku();

If we execute above code we get errors like this

java.lang.IllegalArgumentException: Cannot deserialize value of type `java.util.ArrayList<Sku>` from Object value (token `JsonToken.START_OBJECT`)
 at [Source: UNKNOWN; byte offset: #UNKNOWN] (through reference chain: Root["skuInventory"]->SkuInventory["sku0"])

How can wee resolve this issue of mapping a json to a pojo which has a property which is dynamic like sku0,sku1,sku2 etc??

I would introduce a Map as follows:

public class Root {
    
    private Map<String, Sku> skuInventory;

}

This Map will then replace your SkuInventory object and has keys like Sku0, Sku1 etc.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM