Anu Martin
Anu Martin

Reputation: 731

Parsing JSON string array to Pojo/Entity for android sqlite database(Room)

I trying to implement a relational database using Room database in android. for example in below json how can i create a entity for genres? or do i need to parse it insert it manually without using 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? = null,

Upvotes: 0

Views: 275

Answers (1)

Erythrozyt
Erythrozyt

Reputation: 804

You need to create a Model for your JSON. Just create a Model manually or go to a site where you can autogenerate models from JSON. JSONSchemaToPojo If you use JSONSchematoPojo you will get something like this:

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. like List<GENRE> genres. If you would like to use an ENUM you have to specifi an Converter for ypur Enum.

    @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();
        }
    }
}

Upvotes: 2

Related Questions