I have very complex JSON response coming from server. I need to insert in local database.
Below is my json response
{
\"currentdate\": \"2018-02-
Generate POJO Class using jsonschema2pojo .
Post your code and filter option right side menu.
----------------------------------com.example.Datum.java-----------------------------------
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Datum {
@SerializedName("date")
@Expose
private String date;
@SerializedName("data")
@Expose
private List<Datum_> data = null;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public List<Datum_> getData() {
return data;
}
public void setData(List<Datum_> data) {
this.data = data;
}
}
-----------------------------------com.example.Datum_.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Datum_ {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("schedulId")
@Expose
private Integer schedulId;
@SerializedName("userId")
@Expose
private Integer userId;
@SerializedName("completedOn")
@Expose
private String completedOn;
@SerializedName("currentDate")
@Expose
private String currentDate;
@SerializedName("workouts")
@Expose
private Workouts workouts;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getSchedulId() {
return schedulId;
}
public void setSchedulId(Integer schedulId) {
this.schedulId = schedulId;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getCompletedOn() {
return completedOn;
}
public void setCompletedOn(String completedOn) {
this.completedOn = completedOn;
}
public String getCurrentDate() {
return currentDate;
}
public void setCurrentDate(String currentDate) {
this.currentDate = currentDate;
}
public Workouts getWorkouts() {
return workouts;
}
public void setWorkouts(Workouts workouts) {
this.workouts = workouts;
}
}
-----------------------------------com.example.ExerciseList.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class ExerciseList {
@SerializedName("exerciseId")
@Expose
private Integer exerciseId;
@SerializedName("name")
@Expose
private String name;
@SerializedName("setReps")
@Expose
private String setReps;
@SerializedName("sort")
@Expose
private Integer sort;
public Integer getExerciseId() {
return exerciseId;
}
public void setExerciseId(Integer exerciseId) {
this.exerciseId = exerciseId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSetReps() {
return setReps;
}
public void setSetReps(String setReps) {
this.setReps = setReps;
}
public Integer getSort() {
return sort;
}
public void setSort(Integer sort) {
this.sort = sort;
}
}
-----------------------------------com.example.Response.java-----------------------------------
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Response {
@SerializedName("currentdate")
@Expose
private String currentdate;
@SerializedName("data")
@Expose
private List<Datum> data = null;
@SerializedName("status")
@Expose
private Integer status;
public String getCurrentdate() {
return currentdate;
}
public void setCurrentdate(String currentdate) {
this.currentdate = currentdate;
}
public List<Datum> getData() {
return data;
}
public void setData(List<Datum> data) {
this.data = data;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
}
-----------------------------------com.example.Stage.java-----------------------------------
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Stage {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("mainExerciseName")
@Expose
private String mainExerciseName;
@SerializedName("exerciseSets")
@Expose
private Integer exerciseSets;
@SerializedName("exerciseList")
@Expose
private List<ExerciseList> exerciseList = null;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getMainExerciseName() {
return mainExerciseName;
}
public void setMainExerciseName(String mainExerciseName) {
this.mainExerciseName = mainExerciseName;
}
public Integer getExerciseSets() {
return exerciseSets;
}
public void setExerciseSets(Integer exerciseSets) {
this.exerciseSets = exerciseSets;
}
public List<ExerciseList> getExerciseList() {
return exerciseList;
}
public void setExerciseList(List<ExerciseList> exerciseList) {
this.exerciseList = exerciseList;
}
}
-----------------------------------com.example.WorkoutDetails.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class WorkoutDetails {
@SerializedName("workoutDetails")
@Expose
private String workoutDetails;
@SerializedName("workoutName")
@Expose
private String workoutName;
@SerializedName("day")
@Expose
private String day;
@SerializedName("inComplete")
@Expose
private String inComplete;
public String getWorkoutDetails() {
return workoutDetails;
}
public void setWorkoutDetails(String workoutDetails) {
this.workoutDetails = workoutDetails;
}
public String getWorkoutName() {
return workoutName;
}
public void setWorkoutName(String workoutName) {
this.workoutName = workoutName;
}
public String getDay() {
return day;
}
public void setDay(String day) {
this.day = day;
}
public String getInComplete() {
return inComplete;
}
public void setInComplete(String inComplete) {
this.inComplete = inComplete;
}
}
-----------------------------------com.example.Workouts.java-----------------------------------
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Workouts {
@SerializedName("workoutDetails")
@Expose
private WorkoutDetails workoutDetails;
@SerializedName("stages")
@Expose
private List<Stage> stages = null;
public WorkoutDetails getWorkoutDetails() {
return workoutDetails;
}
public void setWorkoutDetails(WorkoutDetails workoutDetails) {
this.workoutDetails = workoutDetails;
}
public List<Stage> getStages() {
return stages;
}
public void setStages(List<Stage> stages) {
this.stages = stages;
}
}
Are you using retrofit?
Here is your json:
{
"currentdate": "2018-02-27",
"data": [{
"date": "2017-11-05",
"data": [{
"id": 268,
"schedulId": 268,
"userId": 70,
"completedOn": "0000-00-00 00:00:00",
"currentDate": "2018-02-27",
"workouts": {
"workoutDetails": {
"workoutDetails": "1Day Gain Muscle GYM",
"workoutName": "Gain Muscle",
"day": "Day 1",
"inComplete": "0"
},
"stages": [{
"id": 2,
"mainExerciseName": "Warmup",
"exerciseSets": 1,
"exerciseList": [{
"exerciseId": 602,
"name": "Jumping Jacks",
"setReps": "2X25",
"sort": 0
}]
}]
}
}]
}],
"status": 200
}
Create a model that can serialize that json and then type @Entity (tableName = "myTableName")
above the class name for that model.
@Entity (tableName = "my_complex_model")
public class MyCompledModel {
public String currentDate;
@TypeConverter(ParentDataTypeConverter.class)
public List<ParentData> data;
}
public class ParentData {
public String date;
@TypeConverter(NestedDataTypeConverter.class)
public List<NestedData> data;
}
public class NestedData {
public int id;
public int schedulId;
public int userId;
public String completedOn;
}
etc.. you get the point.
You need to add a typeConverter for the lists so that they know how to populate cell in the db.
public class NestedDataTypeConverter {
private static Gson gson = new Gson();
private static Type type = new TypeToken<List<NestedData>>(){}.getType();
@TypeConverter
public static List<NestedData> stringToNestedData(String json) {
return gson.fromJson(json, type);
}
@TypeConverter
public static String nestedDataToString(List<NestedData> nestedData) {
return gson.toJson(nestedData, type);
}
}
Then you need to create a Dao Interface
@Dao
interface MyComplexModelDao {
@Query("..")
void delete(int id);
@Query("..")
void insert(MyComplexModel model);
}
Then lastly in the Database class you need to annotate the model.
@Database(entities = {
MyComplexModel.class, ... others }
@TypeConverters(value = {
EmployeeSlimTypeConverter.class, ... others }
public abstract class AppDatabase extends RoomDatabase {
public abstract MyComplexModelDao myComplexModelDao();
}
Something like that, don't copy paste. This is just written from my head.
I got this from a website
-----------------------------------com.example.Datum.java-----------------------------------
package com.example;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"date",
"data"
})
public class Datum {
@JsonProperty("date")
private String date;
@JsonProperty("data")
private List<Datum_> data = null;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("date")
public String getDate() {
return date;
}
@JsonProperty("date")
public void setDate(String date) {
this.date = date;
}
@JsonProperty("data")
public List<Datum_> getData() {
return data;
}
@JsonProperty("data")
public void setData(List<Datum_> data) {
this.data = data;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
-----------------------------------com.example.Datum_.java-----------------------------------
package com.example;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"id",
"schedulId",
"userId",
"completedOn",
"currentDate",
"workouts"
})
public class Datum_ {
@JsonProperty("id")
private Integer id;
@JsonProperty("schedulId")
private Integer schedulId;
@JsonProperty("userId")
private Integer userId;
@JsonProperty("completedOn")
private String completedOn;
@JsonProperty("currentDate")
private String currentDate;
@JsonProperty("workouts")
private Workouts workouts;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("id")
public Integer getId() {
return id;
}
@JsonProperty("id")
public void setId(Integer id) {
this.id = id;
}
@JsonProperty("schedulId")
public Integer getSchedulId() {
return schedulId;
}
@JsonProperty("schedulId")
public void setSchedulId(Integer schedulId) {
this.schedulId = schedulId;
}
@JsonProperty("userId")
public Integer getUserId() {
return userId;
}
@JsonProperty("userId")
public void setUserId(Integer userId) {
this.userId = userId;
}
@JsonProperty("completedOn")
public String getCompletedOn() {
return completedOn;
}
@JsonProperty("completedOn")
public void setCompletedOn(String completedOn) {
this.completedOn = completedOn;
}
@JsonProperty("currentDate")
public String getCurrentDate() {
return currentDate;
}
@JsonProperty("currentDate")
public void setCurrentDate(String currentDate) {
this.currentDate = currentDate;
}
@JsonProperty("workouts")
public Workouts getWorkouts() {
return workouts;
}
@JsonProperty("workouts")
public void setWorkouts(Workouts workouts) {
this.workouts = workouts;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
-----------------------------------com.example.Example.java-----------------------------------
package com.example;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"currentdate",
"data",
"status"
})
public class Example {
@JsonProperty("currentdate")
private String currentdate;
@JsonProperty("data")
private List<Datum> data = null;
@JsonProperty("status")
private Integer status;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("currentdate")
public String getCurrentdate() {
return currentdate;
}
@JsonProperty("currentdate")
public void setCurrentdate(String currentdate) {
this.currentdate = currentdate;
}
@JsonProperty("data")
public List<Datum> getData() {
return data;
}
@JsonProperty("data")
public void setData(List<Datum> data) {
this.data = data;
}
@JsonProperty("status")
public Integer getStatus() {
return status;
}
@JsonProperty("status")
public void setStatus(Integer status) {
this.status = status;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
-----------------------------------com.example.ExerciseList.java-----------------------------------
package com.example;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"exerciseId",
"name",
"setReps",
"sort"
})
public class ExerciseList {
@JsonProperty("exerciseId")
private Integer exerciseId;
@JsonProperty("name")
private String name;
@JsonProperty("setReps")
private String setReps;
@JsonProperty("sort")
private Integer sort;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("exerciseId")
public Integer getExerciseId() {
return exerciseId;
}
@JsonProperty("exerciseId")
public void setExerciseId(Integer exerciseId) {
this.exerciseId = exerciseId;
}
@JsonProperty("name")
public String getName() {
return name;
}
@JsonProperty("name")
public void setName(String name) {
this.name = name;
}
@JsonProperty("setReps")
public String getSetReps() {
return setReps;
}
@JsonProperty("setReps")
public void setSetReps(String setReps) {
this.setReps = setReps;
}
@JsonProperty("sort")
public Integer getSort() {
return sort;
}
@JsonProperty("sort")
public void setSort(Integer sort) {
this.sort = sort;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
-----------------------------------com.example.Stage.java-----------------------------------
package com.example;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"id",
"mainExerciseName",
"exerciseSets",
"exerciseList"
})
public class Stage {
@JsonProperty("id")
private Integer id;
@JsonProperty("mainExerciseName")
private String mainExerciseName;
@JsonProperty("exerciseSets")
private Integer exerciseSets;
@JsonProperty("exerciseList")
private List<ExerciseList> exerciseList = null;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("id")
public Integer getId() {
return id;
}
@JsonProperty("id")
public void setId(Integer id) {
this.id = id;
}
@JsonProperty("mainExerciseName")
public String getMainExerciseName() {
return mainExerciseName;
}
@JsonProperty("mainExerciseName")
public void setMainExerciseName(String mainExerciseName) {
this.mainExerciseName = mainExerciseName;
}
@JsonProperty("exerciseSets")
public Integer getExerciseSets() {
return exerciseSets;
}
@JsonProperty("exerciseSets")
public void setExerciseSets(Integer exerciseSets) {
this.exerciseSets = exerciseSets;
}
@JsonProperty("exerciseList")
public List<ExerciseList> getExerciseList() {
return exerciseList;
}
@JsonProperty("exerciseList")
public void setExerciseList(List<ExerciseList> exerciseList) {
this.exerciseList = exerciseList;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
-----------------------------------com.example.WorkoutDetails.java-----------------------------------
package com.example;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"workoutDetails",
"workoutName",
"day",
"inComplete"
})
public class WorkoutDetails {
@JsonProperty("workoutDetails")
private String workoutDetails;
@JsonProperty("workoutName")
private String workoutName;
@JsonProperty("day")
private String day;
@JsonProperty("inComplete")
private String inComplete;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("workoutDetails")
public String getWorkoutDetails() {
return workoutDetails;
}
@JsonProperty("workoutDetails")
public void setWorkoutDetails(String workoutDetails) {
this.workoutDetails = workoutDetails;
}
@JsonProperty("workoutName")
public String getWorkoutName() {
return workoutName;
}
@JsonProperty("workoutName")
public void setWorkoutName(String workoutName) {
this.workoutName = workoutName;
}
@JsonProperty("day")
public String getDay() {
return day;
}
@JsonProperty("day")
public void setDay(String day) {
this.day = day;
}
@JsonProperty("inComplete")
public String getInComplete() {
return inComplete;
}
@JsonProperty("inComplete")
public void setInComplete(String inComplete) {
this.inComplete = inComplete;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
-----------------------------------com.example.Workouts.java-----------------------------------
package com.example;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"workoutDetails",
"stages"
})
public class Workouts {
@JsonProperty("workoutDetails")
private WorkoutDetails workoutDetails;
@JsonProperty("stages")
private List<Stage> stages = null;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("workoutDetails")
public WorkoutDetails getWorkoutDetails() {
return workoutDetails;
}
@JsonProperty("workoutDetails")
public void setWorkoutDetails(WorkoutDetails workoutDetails) {
this.workoutDetails = workoutDetails;
}
@JsonProperty("stages")
public List<Stage> getStages() {
return stages;
}
@JsonProperty("stages")
public void setStages(List<Stage> stages) {
this.stages = stages;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}