【发布时间】:2017-07-09 06:55:51
【问题描述】:
我列出了我尝试使用 json 序列化的自定义类的对象,但序列化后的值为 0,而不是存储在列表中的实际值。
我的自定义类
public class CustomClass extends RealmObject {
@Expose()
@SerializedName("startID")
private int startMessageID;
@Expose()
@SerializedName("endID")
private int endMessageID;
@Expose(serialize = false)
private boolean syncing = false;
}
以下是我用来序列化列表的内容。
GsonBuilder builder = new GsonBuilder();
builder.excludeFieldsWithoutExposeAnnotation();
Gson gson = builder.create();
Type listType = new TypeToken<List<CustomClass >>() {
}.getType();
Log.i("Json", gson.toJson(syncModelList, listType));
上面的代码产生一个输出
[{"endID":0,"startID":0},{"endID":0,"startID":0}]
结构正确但我的值丢失了,我在序列化之前检查了值,它们是正确的并且存在。
【问题讨论】:
-
看看这个,它可能会有所帮助stackoverflow.com/questions/34230451/…
-
@Snapper,谢谢,如果你把它写成答案,我可以把它标记为正确
标签: android json serialization gson