Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insert JSON data using Room Library

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.

like image 635
Solution Soni Avatar asked Feb 28 '18 10:02

Solution Soni


1 Answers

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.

like image 168
Anes Hasic Avatar answered Sep 17 '22 09:09

Anes Hasic