insert JSON data using Room Library
Asked Answered
B

2

12

I have very complex JSON response coming from server. I need to insert in local database.

Below is my json response

{
  "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
}

How I can make My pojo class with column declearation.

Biphenyl answered 28/2, 2018 at 10:37 Comment(1)
Each json object corresponds to a pojo class, each json array corresponds to a list of pojo class. apply that logicCourtroom
B
20

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.

Bushore answered 28/2, 2018 at 10:46 Comment(1)
hi what if the json has few null values, as when i parse the json the object i receive has null values that room doesnt allow me to insert. how do u manage that ?Fatling
C
-2

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

}
Courtroom answered 28/2, 2018 at 10:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.