gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 13:24:32 29 4
gpt4 key购买 nike

我有一个 List我想存储在 SharedPreferences 中的非通用类型.为此,我使用 Gson 来转换 List到字符串,然后将其存储到 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 fieldList<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"
}
]

我不知道出了什么问题。请帮助我了解为什么会出现此错误以及如何解决它。提前致谢。

最佳答案

你的 JSON 有这个:

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

你的 Java 类有这个:

private Date feedDateTime;

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

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

关于java - 使用 Gson 反序列化对象时字段异常的无效值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22953533/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com