使用Room Library插入JSON数据

时间:2018-02-28 10:37:14

标签: android android-room

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

以下是我的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
}

如何使用柱子declearation制作我的pojo课程。

3 个答案:

答案 0 :(得分:3)

你在使用改造吗?

这是你的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")

@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,以便他们知道如何填充数据库中的单元格。

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接口

@Dao
interface MyComplexModelDao {
    @Query("..")
    void delete(int id);

    @Query("..")
    void insert(MyComplexModel model);
}

然后最后在Database类中需要注释模型。

@Database(entities = { 
    MyComplexModel.class, ... others }

@TypeConverters(value = { 
    EmployeeSlimTypeConverter.class, ... others }

public abstract class AppDatabase extends RoomDatabase {
    public abstract MyComplexModelDao myComplexModelDao();
}

这样的东西,不要复制粘贴。这只是从我的头脑中写的。

答案 1 :(得分:-1)

我是从网站上得到的

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

}

答案 2 :(得分:-2)

使用jsonschema2pojo生成POJO类

发布您的代码和过滤器选项右侧菜单。

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

}