Gson.toString() gives error “IllegalArgumentException: multiple JSON fields named mPaint”

◇◆丶佛笑我妖孽 提交于 2019-12-21 02:49:21

问题


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.

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.

Anyone has any idea?


回答1:


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.

Add below class to to tell GSON to save and/or retrieve only those variables who have Serialized name declared.

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.

class myClass {
    @SerializedName("id")
    int id;
    @SerializedName("name")
    String name;
}

Code to convert myObject to 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 :

Exclude ex = new Exclude();
Gson gson = new GsonBuilder().addDeserializationExclusionStrategy(ex).addSerializationExclusionStrategy(ex).create();
myClass myObject = gson.fromJson(jsonString, myClass.class);



回答2:


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.

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 it the field "netAmountPcy" Class A and you will still have the field (because it extends the class).



来源:https://stackoverflow.com/questions/19315431/gson-tostring-gives-error-illegalargumentexception-multiple-json-fields-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!