我们应该如何处理Room中的嵌套对象?

What should we do for nested objects in Room?(我们应该如何处理Room中的嵌套对象?)

本文介绍了我们应该如何处理Room中的嵌套对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果下面有类似我的JSON结构的结构,我们应该如何创建实体类?没有这样的例子。虽然在很久以前写的文章中,@Embeded用于内部数组,但现在使用的是类似Converter的结构。我们应该用哪一个呢?这些是做什么用的?如何创建我的类型的结构?请使用Java提供帮助

 

此处提供了所有必需的结构:https://github.com/theoyuncu8/roomdb

JSON数据


{
"MyData": [
 {
   "food_id": "1",
   "food_name": "Food 1",
   "food_image": "imageurl",
   "food_kcal": "32",
   "food_url": "url",
   "food_description": "desc",
   "carb_percent": "72",
   "protein_percent": "23",
   "fat_percent": "4",
   "units": [
     {
       "unit": "Unit A",
       "amount": "735.00",
       "calory": "75.757",
       "calcium": "8.580",
       "carbohydrt": "63.363",
       "cholestrl": "63.0",
       "fiber_td": "56.12",
       "iron": "13.0474",
       "lipid_tot": "13.01",
       "potassium": "11.852",
       "protein": "717.1925",
       "sodium": "112.02",
       "vit_a_iu": "110.7692",
       "vit_c": "110.744"
     },
     {
       "unit": "Unit C",
       "amount": "32.00",
       "calory": "23.757",
       "calcium": "53.580",
       "carbohydrt": "39.363",
       "cholestrl": "39.0",
       "fiber_td": "93.12",
       "iron": "93.0474",
       "lipid_tot": "93.01",
       "potassium": "9.852",
       "protein": "72.1925",
       "sodium": "10.0882",
       "vit_a_iu": "80.7692",
       "vit_c": "80.744"
     }
   ]
 },
 {
   "food_id": "2",
   "food_name": "Food 2",
   "food_image": "imageurl",
   "food_kcal": "50",
   "food_url": "url",
   "food_description": "desc",
   "carb_percent": "25",
   "protein_percent": "14",
   "fat_percent": "8",
   "units": [
     {
       "unit": "Unit A",
       "amount": "25.00",
       "calory": "25.757",
       "calcium": "55.580",
       "carbohydrt": "53.363",
       "cholestrl": "53.0",
       "fiber_td": "53.12",
       "iron": "53.0474",
       "lipid_tot": "53.01",
       "potassium": "17.852",
       "protein": "757.1925",
       "sodium": "122.02",
       "vit_a_iu": "10.7692",
       "vit_c": "10.744"
     },
     {
       "unit": "Unit C",
       "amount": "2.00",
       "calory": "2.757",
       "calcium": "5.580",
       "carbohydrt": "3.363",
       "cholestrl": "3.0",
       "fiber_td": "3.12",
       "iron": "3.0474",
       "lipid_tot": "3.01",
       "potassium": "77.852",
       "protein": "77.1925",
       "sodium": "12.02",
       "vit_a_iu": "0.7692",
       "vit_c": "0.744"
     },
     {
       "unit": "Unit G",
       "amount": "1.00",
       "calory": "2.1",
       "calcium": "0.580",
       "carbohydrt": "0.363",
       "cholestrl": "0.0",
       "fiber_td": "0.12",
       "iron": "0.0474",
       "lipid_tot": "0.01",
       "potassium": "5.852",
       "protein": "0.1925",
       "sodium": "1.02",
       "vit_a_iu": "0.7692",
       "vit_c": "0.744"
     }
   ]
 }
]
}

实体类

食品类

public class Foods {
    @SerializedName("food_id")
    @Expose
    private String foodId;
    @SerializedName("food_name")
    @Expose
    private String foodName;
    @SerializedName("food_image")
    @Expose
    private String foodImage;
    @SerializedName("food_kcal")
    @Expose
    private String foodKcal;
    @SerializedName("food_url")
    @Expose
    private String foodUrl;
    @SerializedName("food_description")
    @Expose
    private String foodDescription;
    @SerializedName("carb_percent")
    @Expose
    private String carbPercent;
    @SerializedName("protein_percent")
    @Expose
    private String proteinPercent;
    @SerializedName("fat_percent")
    @Expose
    private String fatPercent;

// here

    @SerializedName("units")
    @Expose
    private List<FoodUnitsData> units = null;

    // getter setter

}

FoodUnitsData类

public class FoodUnitsData {
   @SerializedName("unit")
   @Expose
   private String unit;
   @SerializedName("amount")
   @Expose
   private String amount;
   @SerializedName("calory")
   @Expose
   private String calory;
   @SerializedName("calcium")
   @Expose
   private String calcium;
   @SerializedName("carbohydrt")
   @Expose
   private String carbohydrt;
   @SerializedName("cholestrl")
   @Expose
   private String cholestrl;
   @SerializedName("fiber_td")
   @Expose
   private String fiberTd;
   @SerializedName("iron")
   @Expose
   private String iron;
   @SerializedName("lipid_tot")
   @Expose
   private String lipidTot;
   @SerializedName("potassium")
   @Expose
   private String potassium;
   @SerializedName("protein")
   @Expose
   private String protein;
   @SerializedName("sodium")
   @Expose
   private String sodium;
   @SerializedName("vit_a_iu")
   @Expose
   private String vitAIu;
   @SerializedName("vit_c")
   @Expose
   private String vitC;


   // getter setter
}

推荐答案

这些是做什么的?

TypeConverter用于将Room无法处理的类型转换为它可以处理的类型(字符串、基元、整数类型(如Integer、Long)、小数类型(如Double、Float))。

@Embedded基本上表示包含@Embedded类的成员变量作为列。例如@Embedded FoodUnitsData foodUnitsData;

从房间角度测试/验证架构

使用上面的类以及用@Database(FoodDatabase)注释的类中定义的实体,最好编译/生成项目并修复Room抱怨的任何问题(在本例中没有)。

因此有FoodDataabse为:-

@Database(entities = {Foods.class, FoodUnitsDataEntity.class /*<<<<<<<<<< ADDED*

本文标题为:我们应该如何处理Room中的嵌套对象?