使用Gson反序列化对象时,字段异常的值无效

时间:2014-04-09 05:29:00

标签: java android json arraylist gson

我有List非通用类型,我想将其存储在SharedPreferences中。为此,我使用 Gson List转换为String,然后将其存储到SharedPreferences,如下所示:

Gson gson = new Gson();
Type fooType = new TypeToken<ArrayList<UserFeedMaster>>() {}.getType();
// feedTempList is the ArrayList of type UserFeedMaster class
String gsonFeeds = gson.toJson(feedTempList,fooType);
prefs.edit().putString("recent_feedlist", gsonFeeds).commit();

稍后我试图以这种方式将值恢复为ArrayList<UserFeedMaster>

String gsonFeed = prefs.getString("recent_feedlist", null);
Gson gson = new Gson();
Type type = new TypeToken<ArrayList<UserFeedMaster>>(){}.getType();
List<UserFeedMaster> historyFeeds = gson.fromJson(gsonFeed,type);

对于序列化数据,它工作正常。但问题是,当我对其进行反序列化时,我在第java.lang.IllegalArgumentException: invalid value for field行收到错误List<UserFeedMaster> historyFeeds = gson.fromJson(gsonFeed,type);

错误日志:

java.lang.IllegalArgumentException: invalid value for field
at java.lang.reflect.Field.setField(Native Method)
at java.lang.reflect.Field.set(Field.java:588)
at com.google.api.client.util.FieldInfo.setFieldValue(FieldInfo.java:245)
at com.google.api.client.util.FieldInfo.setValue(FieldInfo.java:206)
at com.google.api.client.util.GenericData.put(GenericData.java:103)
at com.google.api.client.util.GenericData.put(GenericData.java:47)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:189)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:146)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
at com.google.gson.Gson.fromJson(Gson.java:755)
at com.google.gson.Gson.fromJson(Gson.java:721)
at com.google.gson.Gson.fromJson(Gson.java:670)
at com.myapp.sample.RecentFeedsFragment.onActivityCreated(RecentFeedsFragment.java:178)`

UserFeedMaster.java

 public class UserFeedMaster {

    @Id
    private String feedBlobKey;
    private Date feedDateTime;
    private int feedLikes;
    private boolean feedIsPrivate;
    private boolean userIsAnonymous;
    private String feedTags;
    private boolean isFeedDeleted;
    private double latitude;
    private double longitude;

    public double getLatitude() {
        return latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public String getFeedBlobKey() {
        return feedBlobKey;
    }

    public void setFeedBlobKey(String feedBlobKey) {
        this.feedBlobKey = feedBlobKey;
    }

    public Date getFeedDateTime() {
        return feedDateTime;
    }

    public void setFeedDateTime(Date feedDateTime) {
        this.feedDateTime = feedDateTime;
    }


    public int getFeedLikes() {
        return feedLikes;
    }

    public void setFeedLikes(int feedLikes) {
        this.feedLikes = feedLikes;
    }

    public boolean isFeedIsPrivate() {
        return feedIsPrivate;
    }

    public void setFeedIsPrivate(boolean feedIsPrivate) {
        this.feedIsPrivate = feedIsPrivate;
    }

    public boolean isUserIsAnonymous() {
        return userIsAnonymous;
    }

    public void setUserIsAnonymous(boolean userIsAnonymous) {
        this.userIsAnonymous = userIsAnonymous;
    }

    public String getFeedTags() {
        return feedTags;
    }

    public void setFeedTags(String feedTags) {
        this.feedTags = feedTags;
    }

    public boolean isFeedDeleted() {
        return isFeedDeleted;
    }

    public void setFeedDeleted(boolean isFeedDeleted) {
        this.isFeedDeleted = isFeedDeleted;
    }

}

JSON响应:

[
    {
        "feedBlobKey": "AMIfv944GCtKglU0zOhVTq6F0dG9Aj1LxtIN5Qz0d3CuaRWO3MWIXd_1eCBxVJA_T6FNjx83hq-ORmnAoivTz2IxL120iQYtePBUPoTru6sxKj5iLZmkRxqaIodwEgknUQPvrkEG_37rlUIoycRHUwnPJlmc_6lmtN32tw9-b5NW60wP7u5AHZY",
        "feedDateTime": {
            "value": 1396953609433,
            "tzShift": 0,
            "dateOnly": false
        },
        "feedDeleted": false,
        "feedIsPrivate": false,
        "feedLikes": 0,
        "kind": "userfeedmasterendpoint#resourcesItem"
    },
    {
        "feedBlobKey": "AMIfv944GCtKglU0zOhVTq6F0dG9Aj1LxtIN5Qz0d3CuaRWO3MWIXd_1eCBxVJA_T6FNjx83hq-ORmnAoivTz2IxL120iQYtePBUPoTru6sxKj5iLZmkRxqaIodwEgknUQPvrkEG_37rlUIoycRHUwnPJlmc_6lmtN32tw9-b5NW60wP7u5AHZY",
        "feedDateTime": {
            "value": 1396953609433,
            "tzShift": 0,
            "dateOnly": false
        },
        "feedDeleted": false,
        "feedIsPrivate": false,
        "feedLikes": 0,
        "kind": "userfeedmasterendpoint#resourcesItem"
    }
]

我不知道出了什么问题。请帮助我解决为什么我收到此错误,我该如何解决它。 提前谢谢。

1 个答案:

答案 0 :(得分:2)

你的JSON有这个:

"feedDateTime": {
    "value": 1396953609433,
    "tzShift": 0,
    "dateOnly": false
}

你的Java类有这个:

private Date feedDateTime;

其中哪一个与另一个不一样?这就是产生错误的原因。

GSON不会将该JSON神奇地转换为Date对象。您需要创建与该JSON对象匹配的Java类(FeedDateTime),或者为UserFeedMaster类编写自定义反序列化器。