Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
508 views
in Technique[技术] by (71.8m points)

android - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...