Java 无法反序列化启动外\u对象令牌的实例
我的Json看起来像(并且是不可修改的) JavaPOJO代码如下所示Java 无法反序列化启动外\u对象令牌的实例,java,json,jackson,Java,Json,Jackson,我的Json看起来像(并且是不可修改的) JavaPOJO代码如下所示 public static class SaleLines { @JsonProperty("SaleLine") private SaleLineObject[] saleLineObject; public SaleLineObject[] getSaleLineObject() { return saleLineObject; } public void setSaleLineOb
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));
}
公共静态类行{
@JsonProperty(“销售线”)
私有SaleLineObject[]SaleLineObject;
public SaleLineObject[]getSaleLineObject(){return SaleLineObject;}
public void setSaleLineObject(SaleLineObject[]SaleLineObject){this.SaleLineObject=SaleLineObject;}
}
公共静态类SaleLineObject{
私人销售线;
公共销售线getSaleLine(){
返程销售线;
}
公共无效销售线(SaleLine SaleLine){
this.saleLine=saleLine;
}
}
公共静态类销售线{
@JsonProperty(“itemID”)
私有字符串itemId;//行\u项\u nk
@JsonProperty(“单价”)
私有字符串单价;
....
}
@JsonPropertyOrder({“总计”、“计算SubTotal”、“计算Discount”})
公开静态类销售{
私有字符串saleTotal、calcSubtotal、calcDiscount;
私人增值税;
@JsonProperty(“销售线”)
销售线销售线;
@JsonCreator
公开出售(@JsonProperty(“总计”)字符串saleTotal,
@JsonProperty(“calcSubtotal”)字符串calcSubtotal,
@JsonProperty(“calcDiscount”)字符串calcDiscount,
@JsonProperty(“SaleLines”)SaleLines saleLine,
) {
this.saleTotal=saleTotal;
this.calcSubtotal=calcSubtotal;
this.calcDiscount=calcDiscount;
this.saleLine=saleLine;
setSalesValueWOVat();
}
//二传手
}
@SuppressWarnings({“rawtypes”})
公共静态T readFromJsonAndFillType(
字符串json,
模块模块,
类集合类型,
类元素类型)
抛出JsonParseException、JsonMappingException、IOException{
ObjectMapper objMapper=新的ObjectMapper()
.configure(在未知属性上反序列化feature.FAIL,false);
TypeFactory tf=objMapper.getTypeFactory();
JsonNode node=objMapper.readTree(json.get)(module.jsonFieldName);
返回objMapper.readValue(node.toString(),
tf.ConstructionCollectionType(collectionType,elementType));
}
大体上
ArrayList<Sale> saleList = readFromJsonAndFillType(
saleJSON,
Modules.SALE,
ArrayList.class,
Sale.class);
for (Sale sale: saleList) {
System.out.println(sale.toString());
}
ArrayList saleList=readFromJsonAndFillType(
萨利森,
销售,,
ArrayList.class,
销售类);
出售(出售:saleList){
System.out.println(sale.toString());
}
我知道这个问题已经被问了很多次了,甚至我也从他们那里得到了帮助
但我仍然无法克服这个错误我知道这个问题已经被问过多次了&每个人都用不同的方式解决了这些问题。每当您发现“无法反序列化启动外\u对象令牌的实例”。这通常发生在您试图获取json格式实际上不相同的对象时(意味着json起始对象与您正在转换的对象不同)。
例如:-Json返回的第一个对象是布尔值,但不幸的是,您正在将其转换为列表,那么您将出现此错误。
我建议使用下面的代码查看读取格式,然后根据返回的对象进行转换。
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"); } ObjectMapper ObjectMapper=新的ObjectMapper(); Map empMap=objectMapper.readValue(新文件输入流(“employee.json”)、Map.class); 对于(Map.Entry:empMap.entrySet()) { System.out.println(“\n-------------------------------------\n”+entry.getKey()+”=“+entry.getValue()+”\n”); } 获取密钥并根据返回的对象转换值。
参考:-我知道这个问题已经被问了很多次&每个人都用不同的方法解决了这些问题。每当您发现“无法反序列化启动外\u对象令牌的实例”。这通常发生在您试图获取json格式实际上不相同的对象时(意味着json起始对象与您正在转换的对象不同)。
例如:-Json返回的第一个对象是布尔值,但不幸的是,您正在将其转换为列表,那么您将出现此错误。
我建议使用下面的代码查看读取格式,然后根据返回的对象进行转换。
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"); } ObjectMapper ObjectMapper=新的ObjectMapper(); Map empMap=objectMapper.readValue(新文件输入流(“employee.json”)、Map.class); 对于(Map.Entry:empMap.entrySet()) { System.out.println(“\n-------------------------------------\n”+entry.getKey()+”=“+entry.getValue()+”\n”); } 获取密钥并根据返回的对象转换值。
仅供参考:-请告诉我们您认为如何将JSON反序列化到您的对象树结构。同时向我们展示执行反序列化的代码。@SotiriosDelimanolis请查看编辑您有一个JSON对象
{..}
,并且您正试图将其反序列化到数组。您认为应该如何工作?但是json对象在数组中,这就是为什么我有一个数组,然后是代表json对象的类:/请告诉我们您认为json可以如何反序列化到您的对象树结构中。同时向我们展示执行反序列化的代码。@SotiriosDelimanolis请查看编辑器您有一个json对象{..}
,您试图将其反序列化为数组。你认为应该如何工作?但是json对象在数组中,这就是为什么我有一个数组,然后是代表json对象的类:/