我的模型看起来像这样简化了
@Table
public class Movie extends SugarRecord implements Parcelable {
@SerializedName("id")
@Expose
private Long movieId;
@SerializedName("adult")
@Expose
private Boolean adult;
@SerializedName("backdrop_path")
@Expose
private String backdropPath;
@SerializedName("genre_ids")
@Expose
private List<Integer> genreIds = new ArrayList<>();
@SerializedName("original_language")
@Expose
private String originalLanguage;
@SerializedName("original_title")
@Expose
private String originalTitle;
@SerializedName("overview")
@Expose
private String overview;
@SerializedName("release_date")
@Expose
private String releaseDate;
@SerializedName("poster_path")
@Expose
private String posterPath;
@SerializedName("popularity")
@Expose
private Double popularity;
@SerializedName("title")
@Expose
private String title;
@SerializedName("video")
@Expose
private Boolean video;
@SerializedName("vote_average")
@Expose
private Double voteAverage;
@SerializedName("vote_count")
@Expose
private Integer voteCount;
private Boolean favourite;
public Movie() {
this.favourite = false;
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Movie createFromParcel(Parcel in) {
return new Movie(in);
}
public Movie[] newArray(int size) {
return new Movie[size];
}
};
public Movie(Parcel in) {
this.adult = in.readByte() != 0;
this.movieId = in.readLong();
this.backdropPath = in.readString();
in.readList(this.genreIds, null);
this.originalLanguage = in.readString();
this.originalTitle = in.readString();
this.overview = in.readString();
this.releaseDate = in.readString();
this.posterPath = in.readString();
this.popularity = in.readDouble();
this.title = in.readString();
this.video = in.readByte() != 0;
this.voteAverage = in.readDouble();
this.voteCount = in.readInt();
this.favourite = in.readByte() != 0;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeByte((byte) (this.adult ? 1 : 0));
dest.writeLong(this.movieId);
dest.writeString(this.backdropPath);
dest.writeList(this.genreIds);
dest.writeString(this.originalLanguage);
dest.writeString(this.originalTitle);
dest.writeString(this.overview);
dest.writeString(this.releaseDate);
dest.writeString(this.posterPath);
dest.writeDouble(this.popularity);
dest.writeString(this.title);
dest.writeByte((byte) (this.video ? 1 : 0));
dest.writeDouble(this.voteAverage);
dest.writeInt(this.voteCount);
dest.writeByte((byte) (this.favourite ? 1 : 0));
}
}
错误消息我在Gson尝试转换从改造中收到的结果时得到。
java.lang.IllegalArgumentException: class Movie declares multiple JSON fields named id
当Movie没有扩展SugarRecord时,它完全正常。
我可以在这做什么?
答案 0 :(得分:0)
不要同时使用@Table并扩展SugarRecord(至少不在SugarORM 1.4中)。
答案 1 :(得分:0)
如果您只想考虑标记为@Expose的字段,则需要致电:
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
答案 2 :(得分:0)
声明私有瞬态Long id。
这样,你的id字段就不会被序列化。