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

时间:2015-01-04 18:16:18

标签: java json jackson

我的Json看起来像(和它的不可修改的)

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

java POJO代码看起来像

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

我知道这个问题已被多次询问,甚至我也从中获得了帮助 Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

但我仍然无法解决此错误

1 个答案:

答案 0 :(得分:2)

我知道这个问题已被多次询问&amp;每个人都以不同的方式解决问题。每当您找到“无法反序列化START_OBJECT标记的实例”时。它通常发生在你试图获得json格式实际上不相同的对象时(意味着json起始对象不同,而不是你们正在转换)。
对于Ex: - Json返回第一个对象是布尔值,但遗憾的是您要转换为List&lt; Object&gt;那么你会有这个错误 我建议使用下面的代码来查看格式,而不是按照返回的对象进行转换。

        ObjectMapper objectMapper = new ObjectMapper();
        Map<?,?> empMap = objectMapper.readValue(new FileInputStream("employee.json"),Map.class);

        for (Map.Entry<?,?> entry : empMap.entrySet())
        {
          System.out.println("\n----------------------------\n"+entry.getKey() + "=" + entry.getValue()+"\n");
        }

获取钥匙&amp;根据返回的对象转换值 供参考: - https://dzone.com/articles/processing-json-with-jackson