[英]Parsing JSON string array to Pojo/Entity for android sqlite database(Room)
I trying to implement a relational database using Room database in android. 我试图使用Android中的Room数据库来实现关系数据库。 for example in below json how can i create a entity for genres ?
例如在下面的json中,我如何为流派创建实体? or do i need to parse it insert it manually without using GSON?
还是我需要解析它而不使用GSON手动将其插入?
{
"imdb_code": "tt0419887",
"title": "The Kite Runner",
"title_english": "The Kite Runner",
"title_long": "The Kite Runner (2007)",
"slug": "the-kite-runner-2007",
"year": 2007,
"rating": 7.6,
"runtime": 128,
"genres": [
"Drama",
"Comedy"
]
}
i need serialize class like. 我需要像序列化类。
data class Movie(
@field:SerializedName("small_cover_image")
val smallCoverImage: String? = null,
@field:SerializedName("year")
val year: Int? = null,
@field:SerializedName("description_full")
val descriptionFull: String? = null,
@field:SerializedName("rating")
val rating: Double? = null,
@field:SerializedName("large_cover_image")
val largeCoverImage: String? = null,
@field:SerializedName("title_long")
val titleLong: String? = null,
@field:SerializedName("language")
val language: String? = null,
@field:SerializedName("yt_trailer_code")
val ytTrailerCode: String? = null,
@field:SerializedName("title")
val title: String? = null,
@field:SerializedName("mpa_rating")
val mpaRating: String? = null,
@field:SerializedName("genres")
val genres: List<String?>? = null
)
i need 我需要
@field:SerializedName("genres")
val genres: List<Genres?>? = null,
instead of @field:SerializedName("genres") val genres: List? 而不是@field:SerializedName(“ genres”)val类型:列表? = null,
= null,
You need to create a Model for your JSON. 您需要为JSON创建一个模型。 Just create a Model manually or go to a site where you can autogenerate models from JSON.
只需手动创建模型,或访问可从JSON自动生成模型的站点。 JSONSchemaToPojo If you use JSONSchematoPojo you will get something like this:
JSONSchemaToPojo如果您使用JSONSchematoPojo,您将获得如下内容:
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Movie {
@SerializedName("imdb_code")
@Expose
private String imdbCode;
@SerializedName("title")
@Expose
private String title;
@SerializedName("title_english")
@Expose
private String titleEnglish;
@SerializedName("title_long")
@Expose
private String titleLong;
@SerializedName("slug")
@Expose
private String slug;
@SerializedName("year")
@Expose
private Integer year;
@SerializedName("rating")
@Expose
private Double rating;
@SerializedName("runtime")
@Expose
private Integer runtime;
@SerializedName("genres")
@Expose
private List<String> genres = null;
public String getImdbCode() {
return imdbCode;
}
public void setImdbCode(String imdbCode) {
this.imdbCode = imdbCode;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTitleEnglish() {
return titleEnglish;
}
public void setTitleEnglish(String titleEnglish) {
this.titleEnglish = titleEnglish;
}
public String getTitleLong() {
return titleLong;
}
public void setTitleLong(String titleLong) {
this.titleLong = titleLong;
}
public String getSlug() {
return slug;
}
public void setSlug(String slug) {
this.slug = slug;
}
public Integer getYear() {
return year;
}
public void setYear(Integer year) {
this.year = year;
}
public Double getRating() {
return rating;
}
public void setRating(Double rating) {
this.rating = rating;
}
public Integer getRuntime() {
return runtime;
}
public void setRuntime(Integer runtime) {
this.runtime = runtime;
}
public List<String> getGenres() {
return genres;
}
public void setGenres(List<String> genres) {
this.genres = genres;
}
}
For your Genres you can just create a List<String>
genres or you can have an ENUM Representation for Genres. 对于您的流派,您可以仅创建
List<String>
流派,也可以具有流派的ENUM表示形式。 like List<GENRE>
genres. 像
List<GENRE>
流派。 If you would like to use an ENUM you have to specifi an Converter for ypur Enum. 如果要使用ENUM,则必须为ypur Enum指定一个Converter。
@Database(entities = {
MovieEntity.class
}, version = 1)
@TypeConverters(GenreConverter.class)
public abstract class MovieDatabase extends RoomDatabase {
public abstract MovieDao movieDao();
}
public class GenreConverter {
@TypeConverter
public GENRE fromNumber(Integer value) {
return GENRE.fromNumber(value);
}
@TypeConverter
public Integer toNumber(GENRE type) {
if (type == null) {
return null;
} else {
return type.getNumberRepresentation();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.