gpt4 book ai didi

java - 无法反序列化 START_OBJECT token 之外的实例

转载 作者:行者123 更新时间:2023-12-01 18:20:36 24 4
gpt4 key购买 nike

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

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

最佳答案

我知道这个问题已经被问过多次,每个人都用不同的方式解决了这个问题。每当您发现“无法反序列化 START_OBJECT token 的实例”时。当您尝试获取实际上与 json 格式不同的对象时,通常会发生这种情况(意味着 json 起始对象与你们正在转换的对象不同)。
例如:- 返回第一个对象的 Json 是 boolean 值,但不幸的是您将其转换为 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");        }

获取 key 并根据返回的对象转换值。
供引用:-https://dzone.com/articles/processing-json-with-jackson

关于java - 无法反序列化 START_OBJECT token 之外的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27768759/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com