Gson.toString()提供错误和QUOT;抛出:IllegalArgumentException:命名mPaint&QUOT多个JSON领域; [英] Gson.toString() gives error "IllegalArgumentException: multiple JSON fields named mPaint"

查看:991
本文介绍了Gson.toString()提供错误和QUOT;抛出:IllegalArgumentException:命名mPaint&QUOT多个JSON领域;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自定义对象转换为字符串,并保存在共享preferences这是我的终极目标。我想下面的线,将失败。

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)

我尝试了很多的选择和相信的东西,在自定义对象变量。我们所关注的错误日志 java.lang.IllegalArgumentException:如果类android.text.BoringLayout声明了一个名为mPaint多个JSON领域。不知道是什么mPaint。

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.

任何人有任何想法?

推荐答案

据我观察,如果你找到ANY_VARIABLE_NAME 多JSON领域,那么很可能,它是因为GSON无法对象转换为jsonString和jsonString对象。你可以在下面code尝试解决这个问题。

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.

添加下面的类来告诉GSON保存和/或只检索那些谁序列化的名字声明的变量。

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;
    }
}

下面是你需要保存/恢复的对象的类。 添加 @SerializedName 对于需要保存的变量和/或检索。

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为myObject转换成jsonString:

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,从jsonString得到的对象:

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

这篇关于Gson.toString()提供错误和QUOT;抛出:IllegalArgumentException:命名mPaint&QUOT多个JSON领域;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆