Spring Boot 如何在java中将包含sku0、sku1等动态字段的json数据Map到pojo类中

x8goxv8g  于 2022-12-04  发布在  Spring
关注(0)|答案(1)|浏览(149)

下面是我的api响应

{
    "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
        }
    }
}

从上面的json结构可以看到,有多个属性,如sku0,sku1.sku2
我想把这个json转换成我创建的POJO,如下所示,

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;
    }

}

下面是将jsonMap到POJO的代码

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

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

如果我们执行上面的代码,我们会得到这样的错误

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"])

我们如何解决将jsonMap到具有动态属性(如sku0、sku1、sku2等)的pojo的问题?

3pvhb19x

3pvhb19x1#

我将按如下方式介绍Map:

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

}

然后,此Map将替换您的SkuInventory对象,并具有Sku0、Sku1等关键字。

相关问题