本文介绍了GSON java.lang.IllegalArgumentException:类'xx'声明多个名为'XX'的JSON字段AND St​​ackOverflowError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将映射到非常复杂的Object的sqlResult转换为JSON,以便将其保存到redis数据库中。现在我得到错误

  java.lang.IllegalArgumentException:类'xx'声明多个名为'XX'的JSON字段

如何解决这个问题,而不会像错误'xx'中提到的那样对类进行变换?

或者是其他库可用,支持将对象转换为JSON或从JSON转换对象,并支持多个JSON字段名称,例如json-io?




我使用以下建议的类更新了我的项目 以避免多个JSON字段。



但是现在我又遇到了另一个问题:
嵌套异常是:java.lang.StackOverflowError 那个问题?因为我使用了一个非常大的集合/对象进行转换。

解决方案

您没有发表非常详细的问题,所以我希望这会对你有所帮助:



你可能遇到的问题是该字段已经存在于你扩展的类中。在这种情况下,该字段已经存在于B类中。



说:

  public class A extends B {
private BigDecimal netAmountTcy;
私人BigDecimal netAmountPcy;
private BigDecimal priceTo;
私有String段;

私人BigDecimal taxAmountTcy;
private BigDecimal taxAmountPcy;
私人BigDecimal tradeFeesTcy;
私人BigDecimal tradeFeesPcy;

//上述字段的getter和setter

$ b

其中B类是类似的(当然也许更多重复):

  public class B {
private BigDecimal netAmountPcy;
//上述字段的getter和setter

}

只要删除字段netAmountPcyA类,你仍然有该字段(因为它扩展了类)。

I want to convert a sqlResult mapped to a very complex Object to JSON in order to save it to a redis database a value. Now I'm a getting Error

java.lang.IllegalArgumentException: class 'xx' declares multiple JSON fields named 'XX'

How can I solve this problem without chaging the classes as mentioned in the error 'xx'?
Or are other libs avaiable, that are supporting converting object to and from JSON with supporting of multiple JSON fields names e.g. json-io?


I updated my project with the following suggestedd class class A declares multiple JSON fields in order to avoid multiple JSON fields.

But now I have got a another problem
nested exception is: java.lang.StackOverflowError Any suggestions for that problem? Because I am using a very large collection/object for the convertion.

解决方案

You didn't post a very detailed question so I hope this will help you a bit:

A problem you can have is that the field already exists in a Class that you extend. In this case the field would already exist in Class B.

Say:

public class A extends B {
    private BigDecimal netAmountTcy;
    private BigDecimal netAmountPcy;
    private BigDecimal priceTo;
    private String segment;

    private BigDecimal taxAmountTcy;
    private BigDecimal taxAmountPcy;
    private BigDecimal tradeFeesTcy;
    private BigDecimal tradeFeesPcy;

// getter and setter for the above fields

}

where class B is something like (and maybe more duplicates of course):

public class B {
    private BigDecimal netAmountPcy;
// getter and setter for the above fields

}

Just remove the field "netAmountPcy" Class A and you will still have the field (because it extends the class).

这篇关于GSON java.lang.IllegalArgumentException:类'xx'声明多个名为'XX'的JSON字段AND St​​ackOverflowError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 15:18
查看更多