Gson parsing report exception declares multiple JSON fields named `

GSON reports an error declares multiple JSON fields named:

background:

We often use Gson to explain in the development process, so the mapping is very convenient, but in the process of class use, we often use inheritance, if A is the parent class, A class has a field a, if B class inherits A class , and class B also defines a

Inheritance also involves the problem of coverage and sum, that is, the subclass and the parent class can use a method or variable at the same time, but there is no problem in the process of class inheritance, but Gson will have problems when parsing

public class A{

String a="";

String b="";

}

 

public class B extands A{

String c="";

string a="";

}

 

After the merger:

public class B{

String a="";

String b="";

String c="";

}

Gson:

Gson scans variables when parsing, which will cause a to be repeated, and an error will be reported: declares multiple JSON fields named. This exception can also be entertained in the Gson source code, and fields are abnormal.

At this time, we need to exclude the inherited class, whether the parent class and the subclass have the same variable definition, if so, basically need to delete the change of the subclass, and use the parent class.

 

 

 

Guess you like

Origin blog.csdn.net/qq36246172/article/details/116701091