我正在尝试将JsonString转换为Java对象。
json对象
{
"action": "added",
"data": {
"Quote": {
"TotalDiscountsAmount": 0,
"Id": "test123"
},
"Owner": {
"Username": "00000000",
"Id": "00000000"
},
"Discount_Amount__c": 0,
"Base_List_Price__c": 574.88,
"TotalList": 574.88,
"Id": "000000",
"ExtendedTotalList": 574.88,
"BaseListPrice": 474.88
}
}
我的POJOs:
AddtocartJson.java地址
package com.product.json;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
"action",
"data"
})
public class AddToCartJson {
@JsonProperty("action")
private String action;
@JsonProperty("data")
private Data data;
@JsonProperty("action")
public String getAction() {
return action;
}
@JsonProperty("action")
public void setAction(String action) {
this.action = action;
}
@JsonProperty("data")
public Data getData() {
return data;
}
@JsonProperty("data")
public void setData(Data data) {
this.data = data;
}
}
Data.java
package com.product.json;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
"Quote",
"Owner",
"Discount_Amount__c",
"Base_List_Price__c",
"TotalList",
"Id",
"ExtendedTotalList",
"BaseListPrice"
})
public class Data {
@JsonProperty("Quote")
private com.product.json.Quote Quote;
@JsonProperty("Owner")
private com.product.json.Owner Owner;
@JsonProperty("Discount_Amount__c")
private Integer DiscountAmountC;
@JsonProperty("Base_List_Price__c")
private Double BaseListPriceC;
@JsonProperty("TotalList")
private Double TotalList;
@JsonProperty("Id")
private String Id;
@JsonProperty("ExtendedTotalList")
private Double ExtendedTotalList;
@JsonProperty("BaseListPrice")
private Double BaseListPrice;
@JsonProperty("Quote")
public com.product.json.Quote getQuote() {
return Quote;
}
@JsonProperty("Quote")
public void setQuote(com.product.json.Quote Quote) {
this.Quote = Quote;
}
@JsonProperty("Owner")
public com.product.json.Owner getOwner() {
return Owner;
}
@JsonProperty("Owner")
public void setOwner(com.product.json.Owner Owner) {
this.Owner = Owner;
}
@JsonProperty("Discount_Amount__c")
public Integer getDiscountAmountC() {
return DiscountAmountC;
}
@JsonProperty("Discount_Amount__c")
public void setDiscountAmountC(Integer DiscountAmountC) {
this.DiscountAmountC = DiscountAmountC;
}
@JsonProperty("Base_List_Price__c")
public Double getBaseListPriceC() {
return BaseListPriceC;
}
@JsonProperty("Base_List_Price__c")
public void setBaseListPriceC(Double BaseListPriceC) {
this.BaseListPriceC = BaseListPriceC;
}
@JsonProperty("TotalList")
public Double getTotalList() {
return TotalList;
}
@JsonProperty("TotalList")
public void setTotalList(Double TotalList) {
this.TotalList = TotalList;
}
@JsonProperty("Id")
public String getId() {
return Id;
}
@JsonProperty("Id")
public void setId(String Id) {
this.Id = Id;
}
@JsonProperty("ExtendedTotalList")
public Double getExtendedTotalList() {
return ExtendedTotalList;
}
@JsonProperty("ExtendedTotalList")
public void setExtendedTotalList(Double ExtendedTotalList) {
this.ExtendedTotalList = ExtendedTotalList;
}
@JsonProperty("BaseListPrice")
public Double getBaseListPrice() {
return BaseListPrice;
}
@JsonProperty("BaseListPrice")
public void setBaseListPrice(Double BaseListPrice) {
this.BaseListPrice = BaseListPrice;
}
}
Quote.java
package com.product.json;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
"TotalDiscountsAmount",
"Id"
})
public class Quote {
@JsonProperty("TotalDiscountsAmount")
private Integer TotalDiscountsAmount;
@JsonProperty("Id")
private String Id;
@JsonProperty("TotalDiscountsAmount")
public Integer getTotalDiscountsAmount() {
return TotalDiscountsAmount;
}
@JsonProperty("TotalDiscountsAmount")
public void setTotalDiscountsAmount(Integer TotalDiscountsAmount) {
this.TotalDiscountsAmount = TotalDiscountsAmount;
}
@JsonProperty("Id")
public String getId() {
return Id;
}
@JsonProperty("Id")
public void setId(String Id) {
this.Id = Id;
}
}
当我尝试使用映射器将JSON转换为Java类时,它会抛出一个未识别的属性异常
。似乎就像即使我有一个Quote
对象,映射器也无法识别该对象。
org.codehaus.ackson.map.exc.UnrecognizedPropertyException:无法识别的字段“Quote”(Class.com.product.json.Data),在[Source:java.io.StringReader@11547748; 行:1,列:36](通过引用链:com.product.json.AddToCartJson[“data”]-
我该如何解决这个问题?
这里的问题是OP混合了两个不同的杰克逊版本:
org.codehaus
(org.codehaus.jackson.map.exc.UnrecognizedPropertyException
显示)com.fasterxml
(在importsimport com.fasterxml.jackson.annotation.
中可见)正如 sam 在评论中指出的那样,OP 应该只使用一个版本。要么使用 2.x 进行反序列化,要么使用旧 1.9 版本中的注释。但建议使用 com.fasterxml
中的最新版本,而不是旧版本。
您可能希望通过使用 Jackson 的类级注释来忽略未定义的属性:
@JsonIgnoreProperties