Java Gson.toString() 给出错误“IllegalArgumentException:多个名为 mPaint 的 JSON 字段”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19315431/
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
Gson.toString() gives error "IllegalArgumentException: multiple JSON fields named mPaint"
提问by Geek
I want to convert a custom object into a string and save in SharePreferences which is my ultimate goal. I tried below line which fails.
我想将自定义对象转换为字符串并保存在 SharePreferences 中,这是我的最终目标。我尝试了以下失败的行。
String matchString = gson.toJson(userMatches);
Logcat :
日志:
10-11 15:24:33.245: E/AndroidRuntime(21427): FATAL EXCEPTION: main
10-11 15:24:33.245: E/AndroidRuntime(21427): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=4001, result=-1, data=null}
to activity {com.objectlounge.ridesharebuddy/com.objectlounge.ridesharebuddy.activities.RS_CreateTripActivity}:
java.lang.IllegalArgumentException: class android.text.BoringLayout declares multiple JSON fields named mPaint
10-11 15:24:33.245: E/AndroidRuntime(21427): at android.app.ActivityThread.deliverResults(ActivityThread.java:3302)
I tried a lot of options and believe that something with variables in custom object. Thing to focus in error log is java.lang.IllegalArgumentException: class android.text.BoringLayout declares multiple JSON fields named mPaint
. Don't know what is mPaint.
我尝试了很多选项,并相信自定义对象中带有变量的东西。错误日志中要关注的是java.lang.IllegalArgumentException: class android.text.BoringLayout declares multiple JSON fields named mPaint
. 不知道什么是mPaint。
Anyone has any idea?
任何人有任何想法?
采纳答案by Geek
According to my observation if you find multiple JSON fields for ANY_VARIABLE_NAME
, then it is likely that it is because GSON is not able to convert object to jsonString and jsonString to object. And you can try below code to solve it.
根据我的观察,如果您发现multiple JSON fields for ANY_VARIABLE_NAME
,那么很可能是因为 GSON 无法将 object 转换为 jsonString 和 jsonString 为 object。你可以尝试下面的代码来解决它。
Add below class to to tell GSON to save and/or retrieve only those variables who have Serialized name declared.
添加以下类以告诉 GSON 仅保存和/或检索那些已声明序列化名称的变量。
class Exclude implements ExclusionStrategy {
@Override
public boolean shouldSkipClass(Class<?> arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean shouldSkipField(FieldAttributes field) {
SerializedName ns = field.getAnnotation(SerializedName.class);
if(ns != null)
return false;
return true;
}
}
Below is the class whose object you need to save/retrieve.
Add @SerializedName
for variables that needs to saved and/or retrieved.
下面是您需要保存/检索其对象的类。添加@SerializedName
需要保存和/或检索的变量。
class myClass {
@SerializedName("id")
int id;
@SerializedName("name")
String name;
}
Code to convert myObject to jsonString :
将 myObject 转换为 jsonString 的代码:
Exclude ex = new Exclude();
Gson gson = new GsonBuilder().addDeserializationExclusionStrategy(ex).addSerializationExclusionStrategy(ex).create();
String jsonString = gson.toJson(myObject);
Code to get object from jsonString :
从 jsonString 获取对象的代码:
Exclude ex = new Exclude();
Gson gson = new GsonBuilder().addDeserializationExclusionStrategy(ex).addSerializationExclusionStrategy(ex).create();
myClass myObject = gson.fromJson(jsonString, myClass.class);
回答by Bart Burg
It seems your problem is already solved but this is for people that didn't quite get their answer (like me):
看来你的问题已经解决了,但这适用于没有得到答案的人(比如我):
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 it the field "netAmountPcy" Class A and you will still have the field (because it extends the class).
只需将字段“netAmountPcy”A 类删除,您仍将拥有该字段(因为它扩展了类)。