将JSON字符串数组解析为Android sqlite数据库的Pojo / Entity(Room)

时间:2019-04-18 07:58:39

标签: android gson retrofit android-room

我试图使用android中的Room数据库来实现关系数据库。 例如在下面的json中,我如何为类型创建实体?还是我需要解析它而不使用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"
    ]
}

我需要像序列化类。

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
)

我需要

 @field:SerializedName("genres")
    val genres: List<Genres?>? = null,

而不是     @field:SerializedName(“ genres”)     val类型:清单? = null,

1 个答案:

答案 0 :(得分:2)

您需要为JSON创建一个模型。只需手动创建模型,或访问可从JSON自动生成模型的站点。 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;
}

}

对于您的流派,您可以仅创建List<String>流派,也可以具有流派的ENUM表示形式。像List<GENRE>类型。如果要使用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();
        }
    }
}