Json 到 Java 对象 - 由于字段相同而失败
Json to Java Object - Failed due to identical fields
我正在尝试将 json 转换为 java 对象。由于 json 中有相同的字段,它会抛出这样的错误。
com.fasterxml.jackson.databind.JsonMappingException: Conflicting setter definitions for property "amount":
com.gateway.model.Order#setAmount(1 params) vs com.gateway.model.Order#setAmount(1 params)
这是json(与我的问题相关的部分)
"order":{
"amount":1.000,
"chargeback":{
"amount":0,
"currency":"BHD"
},
}
这是 java class 的相关部分。
public class Order
{
private double amount;
Chargeback ChargebackObject;
// Getter Methods
public double getAmount()
{
return amount;
// Setter Methods
public void setAmount(double amount)
{
this.amount = amount;
}
}
class Chargeback
{
private double amount;
private String currency;
// Getter Methods
@JsonIgnore
public double getAmount()
{
return amount;
}
@JsonInclude(Include.NON_NULL)
public String getCurrency()
{
return currency;
}
// Setter Methods
public void setAmount(double cb_amount)
{
this.amount = cb_amount;
}
public void setCurrency(String currency)
{
this.currency = currency;
}
}
请注意,退款 class 在 Order.java 文件中。
我已尝试 @JsonIgnore
注释并删除 chargeback
class 中的 amount
但错误仍然存在。有人可以为此提出解决方案吗?
我修改了你的代码,试试下面的代码。基本上,我做了以下这些。
- Order 中没有 getter 和 setter 的 Chargeback 对象,所以添加了它。
- 注释掉@Json忽略注释。
课程如下
class Chargeback {
private double amount;
private String currency;
// Getter Methods
// @JsonIgnore
public double getAmount() {
return amount;
}
// @JsonInclude(Include.NON_NULL)
public String getCurrency() {
return currency;
}
// Setter Methods
public void setAmount(double cb_amount) {
this.amount = cb_amount;
}
public void setCurrency(String currency) {
this.currency = currency;
}
}
public class Order {
private double amount;
Chargeback ChargebackObject;
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public Chargeback getChargebackObject() {
return ChargebackObject;
}
public void setChargebackObject(Chargeback chargebackObject) {
ChargebackObject = chargebackObject;
}
}
生成 Json 的测试代码如下。
public class Test1 {
public static void main(String[] args) throws Exception {
Chargeback chargeback = new Chargeback();
chargeback.setAmount(1234.00);
chargeback.setCurrency("BHD");
Order order = new Order();
order.setAmount(2345.00);
order.setChargebackObject(chargeback);
ObjectMapper mapper = new ObjectMapper();
String toJson = null;
try {
toJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(order);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Complete Json = " + toJson);
// From Json String to Java Object
ObjectMapper mapper1 = new ObjectMapper();
Order order1 = mapper.readValue(toJson, Order.class);
System.out.println("Order Object -> " + order1);
}
}
生成json如下。
{
"amount" : 2345.0,
"chargebackObject" : {
"amount" : 1234.0,
"currency" : "BHD"
}
}
我正在尝试将 json 转换为 java 对象。由于 json 中有相同的字段,它会抛出这样的错误。
com.fasterxml.jackson.databind.JsonMappingException: Conflicting setter definitions for property "amount":
com.gateway.model.Order#setAmount(1 params) vs com.gateway.model.Order#setAmount(1 params)
这是json(与我的问题相关的部分)
"order":{
"amount":1.000,
"chargeback":{
"amount":0,
"currency":"BHD"
},
}
这是 java class 的相关部分。
public class Order
{
private double amount;
Chargeback ChargebackObject;
// Getter Methods
public double getAmount()
{
return amount;
// Setter Methods
public void setAmount(double amount)
{
this.amount = amount;
}
}
class Chargeback
{
private double amount;
private String currency;
// Getter Methods
@JsonIgnore
public double getAmount()
{
return amount;
}
@JsonInclude(Include.NON_NULL)
public String getCurrency()
{
return currency;
}
// Setter Methods
public void setAmount(double cb_amount)
{
this.amount = cb_amount;
}
public void setCurrency(String currency)
{
this.currency = currency;
}
}
请注意,退款 class 在 Order.java 文件中。
我已尝试 @JsonIgnore
注释并删除 chargeback
class 中的 amount
但错误仍然存在。有人可以为此提出解决方案吗?
我修改了你的代码,试试下面的代码。基本上,我做了以下这些。
- Order 中没有 getter 和 setter 的 Chargeback 对象,所以添加了它。
- 注释掉@Json忽略注释。
课程如下
class Chargeback {
private double amount;
private String currency;
// Getter Methods
// @JsonIgnore
public double getAmount() {
return amount;
}
// @JsonInclude(Include.NON_NULL)
public String getCurrency() {
return currency;
}
// Setter Methods
public void setAmount(double cb_amount) {
this.amount = cb_amount;
}
public void setCurrency(String currency) {
this.currency = currency;
}
}
public class Order {
private double amount;
Chargeback ChargebackObject;
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public Chargeback getChargebackObject() {
return ChargebackObject;
}
public void setChargebackObject(Chargeback chargebackObject) {
ChargebackObject = chargebackObject;
}
}
生成 Json 的测试代码如下。
public class Test1 {
public static void main(String[] args) throws Exception {
Chargeback chargeback = new Chargeback();
chargeback.setAmount(1234.00);
chargeback.setCurrency("BHD");
Order order = new Order();
order.setAmount(2345.00);
order.setChargebackObject(chargeback);
ObjectMapper mapper = new ObjectMapper();
String toJson = null;
try {
toJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(order);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Complete Json = " + toJson);
// From Json String to Java Object
ObjectMapper mapper1 = new ObjectMapper();
Order order1 = mapper.readValue(toJson, Order.class);
System.out.println("Order Object -> " + order1);
}
}
生成json如下。
{
"amount" : 2345.0,
"chargebackObject" : {
"amount" : 1234.0,
"currency" : "BHD"
}
}