【问题标题】:Can not deserialize instance of out of START_OBJECT token无法反序列化超出 START_OBJECT 令牌的实例
【发布时间】:2015-03-02 08:31:19
【问题描述】:

我的 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

但我仍然无法解决这个错误

【问题讨论】:

  • 请告诉我们您认为 JSON 可以如何反序列化为您的对象树结构。
  • 也向我们展示执行反序列化的代码。
  • @SotiriosDelimanolis 请查看编辑
  • 您有一个 JSON 对象 {..} 并且您正尝试将其反序列化为一个数组。你认为这应该如何运作?
  • 但是 json 对象在数组中,这就是为什么我有一个数组,然后是代表 json 对象的类:/

标签: java json jackson


【解决方案1】:

我知道这个问题已经被问过很多次了,每个人都以不同的方式解决了这个问题。每当您发现“无法反序列化超出 START_OBJECT 令牌的实例”。当您尝试获取 json 格式实际上不相同的对象时,通常会发生这种情况(意味着 json 起始对象与你们转换的对象不同)。
例如:- Json 返回第一个对象是布尔值,但不幸的是你正在转换为 List 那么你会遇到这个错误。
我建议看一下使用下面的代码读取格式,而不是根据返回的对象进行转换。

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

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

【讨论】:

    猜你喜欢
    • 2016-09-25
    • 2019-03-06
    • 2021-09-05
    • 2020-07-22
    • 2019-06-01
    • 2019-12-22
    • 2019-02-12
    • 2014-01-17
    • 2013-10-23
    相关资源
    最近更新 更多