GSON java.lang.IllegalArgumentException:类“xx”声明了多个名为“XX”的 JSON 字段和 StackOverflowError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24820389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:01:19  来源:igfitidea点击:

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

javajsongson

提问by imalik8088

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

我想将映射到非常复杂的对象的 sqlResult 转换为 JSON,以便将其保存到 redis 数据库中的一个值。现在我遇到了错误

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?

如何在不更改错误“xx”中提到的类的情况下解决此问题?
或者其他库是否可用,支持将对象转换为 JSON 并支持多个 JSON 字段名称,例如 json-io?



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

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

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

但现在我有另一个问题
嵌套异常是: java.lang.StackOverflowError对这个问题有什么建议吗?因为我正在使用一个非常大的集合/对象进行转换。

回答by Bart Burg

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.

您可能遇到的一个问题是该字段已存在于您扩展的类中。在这种情况下,该字段已经存在于 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):

其中 B 类类似于(当然可能还有更多重复项):

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).

只需删除字段“netAmountPcy”A 类,您仍将拥有该字段(因为它扩展了类)。