当前位置: 首页 > 知识库问答 >
问题:

无法反序列化未使用START_OBJECT标记的实例

晋涛
2023-03-14

我的Json看起来类似于(并且它是不可修改的)

{
    ....
    "Sale": [
        "SaleLines": {
                    "SaleLine": [
                        {
                            "Item": {
                                "Prices": {
                                    "ItemPrice": [
                                        {
                                            "amount": "100",
                                            "useType": "Default"
                                        },
                                        {
                                            "amount": "100",
                                            "useType": "MSRP"
                                        }
                                    ]
                                },
                            }
                                ......
                                ......
                        } 
                ] 
            "calcDiscount": "0",
            "calcSubtotal": "500",
        }
    ]
}
public static class SaleLines {

    @JsonProperty("SaleLine")
    private SaleLineObject[] saleLineObject;

    public SaleLineObject[] getSaleLineObject() { return saleLineObject; }

    public void setSaleLineObject(SaleLineObject[] saleLineObject) { this.saleLineObject = saleLineObject; }
}

public static class SaleLineObject {
    private SaleLine saleLine;

    public SaleLine getSaleLine() {
        return saleLine;
    }

    public void setSaleLine(SaleLine saleLine) {
        this.saleLine = saleLine;
    }

}

public static class SaleLine {
    @JsonProperty("itemID")
    private String itemId;                  //line_item_nk
    @JsonProperty("unitPrice")
    private String unitPrice;
    ....
}

@JsonPropertyOrder({"total", "calcSubTotal", "calcDiscount"})
public static class Sale {

    private String saleTotal, calcSubtotal, calcDiscount; 
    private int salesValueWOVat;

    @JsonProperty("SaleLines")
    SaleLines saleLine;

    @JsonCreator
    public Sale (@JsonProperty("total")String saleTotal,
            @JsonProperty("calcSubtotal")String calcSubtotal,
            @JsonProperty("calcDiscount")String calcDiscount,
            @JsonProperty("SaleLines")SaleLines saleLine,
    ) {
        this.saleTotal = saleTotal;
        this.calcSubtotal = calcSubtotal;
        this.calcDiscount = calcDiscount;
        this.saleLine = saleLine;
        setSalesValueWOVat();
    }

    // getter and setters 

}

@SuppressWarnings({ "rawtypes" })
public static <E, T extends Collection> T readFromJsonAndFillType (
        String json, 
        Modules module,
        Class <T> collectionType,
        Class <E> elementType) 
        throws JsonParseException, JsonMappingException, IOException {

    ObjectMapper objMapper = new ObjectMapper()
        .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    TypeFactory tf = objMapper.getTypeFactory();
    JsonNode node = objMapper.readTree(json).get(module.jsonFieldName); 
    return objMapper.readValue(node.toString(),
            tf.constructCollectionType(collectionType, elementType));

}
ArrayList<Sale> saleList = readFromJsonAndFillType(
                saleJSON, 
                Modules.SALE, 
                ArrayList.class,
                Sale.class);

for (Sale sale: saleList) {
    System.out.println(sale.toString());
}

我知道这个问题已经被问了很多次了,甚至我也得到了一些帮助,例如:无法反序列化START_OBJECT令牌之外的java.util.ArrayList实例

但我还是无法克服这个错误

共有2个答案

杜鸿彩
2023-03-14
热门标签
公羊喜
2023-03-14
相关问题
 类似资料: