我正在尝试将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
}
}
我的POJO:
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;
}
}
当我尝试使用mapper将JSON转换为Java类时,它会抛出UnrecognizedPropertyException
。看起来很喜欢即使我有一个Quote
对象,某种程度上mapper无法识别该对象。
org.codehaus.jackson.map.exc.UnrecognizedPropertyException:无法识别的字段“Quote”(类com.product.json.Data),未标记为可忽略 在[来源:java.io.StringReader@11547748; line:1,column:36](通过参考链:com.product.json.AddToCartJson [“data”] - > com.product.json.Data [“Quote”]) at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53) at org.codehaus.jackson.map.deser.StdDeserializationContext.unknownFieldException(StdDeserializationContext.java:267) 在org.codehaus.jackson.map.deser.std.StdDeserializer.reportUnknownProperty(StdDeserializer.java:673)
我该如何解决这个问题?
答案 0 :(得分:4)
这里的问题是OP混合了两种不同的Jackson版本:
org.codehaus
(org.codehaus.jackson.map.exc.UnrecognizedPropertyException
显示)com.fasterxml
(在导入import com.fasterxml.jackson.annotation.
中可见)正如sam在评论中所指出的,OP应该只使用一个版本。使用2.x进行反序列化或使用旧1.9版本的注释。
但建议使用com.fasterxml
中的最新版本,而不是旧版本。
答案 1 :(得分:1)
您可能希望使用Jackson的类级别注释来忽略未定义的属性:
@JsonIgnoreProperties