使用Room Library插入JSON数据 [英] insert JSON data using Room Library

查看:218
本文介绍了使用Room Library插入JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有来自服务器的非常复杂的JSON响应.我需要在本地数据库中插入.

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

下面是我的json响应

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
}

我如何使我的pojo类具有列清除功能.

How I can make My pojo class with column declearation.

推荐答案

您是否正在使用改造?

这是您的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
}

创建一个可以序列化json的模型,然后在该模型的类名上方键入@Entity (tableName = "myTableName").

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

等等.你明白了.

您需要为列表添加typeConverter,以便他们知道如何填充数据库中的单元格.

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

然后您需要创建一个Dao界面

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.

这篇关于使用Room Library插入JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆