java 无法反序列化超出 START_OBJECT 令牌的实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27768759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can not deserialize instance of out of START_OBJECT token
提问by mu_sa
My Json looks something like (and its unmodifiable)
我的 Json 看起来像(并且不可修改)
{
....
"Sale": [
"SaleLines": {
"SaleLine": [
{
"Item": {
"Prices": {
"ItemPrice": [
{
"amount": "100",
"useType": "Default"
},
{
"amount": "100",
"useType": "MSRP"
}
]
},
}
......
......
}
]
"calcDiscount": "0",
"calcSubtotal": "500",
}
]
}
The java POJO code looks like
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));
}
In main
在主要
ArrayList<Sale> saleList = readFromJsonAndFillType(
saleJSON,
Modules.SALE,
ArrayList.class,
Sale.class);
for (Sale sale: saleList) {
System.out.println(sale.toString());
}
I know this question has been asked multiple times and even I took help from for eg Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
我知道这个问题已经被问过很多次了,甚至我也得到了帮助,例如 Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
But still I cannot get through this error
但我仍然无法克服这个错误
回答by K_Bist
I know this question has been asked multiple times & everyone getting resolved there problems with different ways. Whenever you find "Can not deserialized instance of out of START_OBJECT token". it's generally occur when you trying to get object which is not actually same in json format (means json starting object is different not as you guys are converting).
For Ex:-Json returning first object is Boolean but unfortunately you are converting is to List<Object> then you will having this error.
I would suggest to have a look to read format using below code than convert it as per the object returning.
我知道这个问题已经被问过多次,每个人都以不同的方式解决了那里的问题。每当您发现"Can not deserialized instance of out of START_OBJECT token" 时。当您尝试获取实际上与 json 格式不同的对象时,通常会发生这种情况(意味着 json 起始对象不同于你们正在转换的对象)。
例如:-Json 返回的第一个对象是布尔值,但不幸的是您正在转换为 List<Object> 那么您将遇到此错误。
我建议看看使用下面的代码读取格式,而不是根据返回的对象进行转换。
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"); }
Get the key & convert the value as per the object returning.
For reference:- https://dzone.com/articles/processing-json-with-Hymanson
获取键并根据返回的对象转换值。
供参考:- https://dzone.com/articles/processing-json-with-Hymanson