How can I store data with nested object data in Android?

I have a high dimensional dataset that it provides with a json structure (about 3000 objects, an array of each object and dozens of objects in that array.) I read them with the help of the volley library. I want to save this data. in the database and access it easily. how can I do it

I couldn’t find examples for Realm

My JSON Data

{
"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"
      }
    ]
  },
  ..............
]
}

AFoodModelList


public class AFoodListModel extends RealmObject {
    public String food_id;
    public String food_name;
    public String food_image;
    public String food_url;
    public String food_kcal;
    public String food_description;
    public String carb_percent;
    public String protein_percent;
    public String fat_percent;
    public RealmList<FoodUnits> food_units;

    public AFoodListModel() {

    }

    public AFoodListModel(String food_id, String food_name, String food_image, String food_url, String food_kcal, String food_description, String carb_percent, String protein_percent, String fat_percent, RealmList<FoodUnits> food_units) {
        this.food_id = food_id;
        this.food_name = food_name;
        this.food_image = food_image;
        this.food_url = food_url;
        this.food_kcal = food_kcal;
        this.food_description = food_description;
        this.carb_percent = carb_percent;
        this.protein_percent = protein_percent;
        this.fat_percent = fat_percent;
        this.food_units = food_units;
    }

    public String getFood_id() {
        return food_id;
    }

    public void setFood_id(String food_id) {
        this.food_id = food_id;
    }

    public String getFood_name() {
        return food_name;
    }

    public void setFood_name(String food_name) {
        this.food_name = food_name;
    }

    public String getFood_image() {
        return food_image;
    }

    public void setFood_image(String food_image) {
        this.food_image = food_image;
    }

    public String getFood_url() {
        return food_url;
    }

    public void setFood_url(String food_url) {
        this.food_url = food_url;
    }

    public String getFood_kcal() {
        return food_kcal;
    }

    public void setFood_kcal(String food_kcal) {
        this.food_kcal = food_kcal;
    }

    public String getFood_description() {
        return food_description;
    }

    public void setFood_description(String food_description) {
        this.food_description = food_description;
    }

    public String getCarb_percent() {
        return carb_percent;
    }

    public void setCarb_percent(String carb_percent) {
        this.carb_percent = carb_percent;
    }

    public String getProtein_percent() {
        return protein_percent;
    }

    public void setProtein_percent(String protein_percent) {
        this.protein_percent = protein_percent;
    }

    public String getFat_percent() {
        return fat_percent;
    }

    public void setFat_percent(String fat_percent) {
        this.fat_percent = fat_percent;
    }

    public RealmList<FoodUnits> getFood_units() {
        return food_units;
    }

    public void setFood_units(RealmList<FoodUnits> food_units) {
        this.food_units = food_units;
    }

}

FoodUnits


@RealmClass(embedded = true)
public class FoodUnits extends RealmObject {
    public String unit;
    public String amount;
    public String calory;
    public String calcium;
    public String carbohydrt;
    public String cholestrl;
    public String fiber_td;
    public String iron;
    public String lipid_tot;
    public String potassium;
    public String protein;
    public String sodium;
    public String vit_a_iu;
    public String vit_c;

//    @LinkingObjects("food_units")
//    public final RealmResults<AFoodListModel> owners=null;

    // @LinkingObjects("food_units")

    public FoodUnits() {
    }

    public FoodUnits(String unit, String amount, String calory, String calcium, String carbohydrt, String cholestrl, String fiber_td, String iron, String lipid_tot, String potassium, String protein, String sodium, String vit_a_iu, String vit_c) {
        this.unit = unit;
        this.amount = amount;
        this.calory = calory;
        this.calcium = calcium;
        this.carbohydrt = carbohydrt;
        this.cholestrl = cholestrl;
        this.fiber_td = fiber_td;
        this.iron = iron;
        this.lipid_tot = lipid_tot;
        this.potassium = potassium;
        this.protein = protein;
        this.sodium = sodium;
        this.vit_a_iu = vit_a_iu;
        this.vit_c = vit_c;
    }

    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }

    public String getCalory() {
        return calory;
    }

    public void setCalory(String calory) {
        this.calory = calory;
    }

    public String getCalcium() {
        return calcium;
    }

    public void setCalcium(String calcium) {
        this.calcium = calcium;
    }

    public String getCarbohydrt() {
        return carbohydrt;
    }

    public void setCarbohydrt(String carbohydrt) {
        this.carbohydrt = carbohydrt;
    }

    public String getCholestrl() {
        return cholestrl;
    }

    public void setCholestrl(String cholestrl) {
        this.cholestrl = cholestrl;
    }

    public String getFiber_td() {
        return fiber_td;
    }

    public void setFiber_td(String fiber_td) {
        this.fiber_td = fiber_td;
    }

    public String getIron() {
        return iron;
    }

    public void setIron(String iron) {
        this.iron = iron;
    }

    public String getLipid_tot() {
        return lipid_tot;
    }

    public void setLipid_tot(String lipid_tot) {
        this.lipid_tot = lipid_tot;
    }

    public String getPotassium() {
        return potassium;
    }

    public void setPotassium(String potassium) {
        this.potassium = potassium;
    }

    public String getProtein() {
        return protein;
    }

    public void setProtein(String protein) {
        this.protein = protein;
    }

    public String getSodium() {
        return sodium;
    }

    public void setSodium(String sodium) {
        this.sodium = sodium;
    }

    public String getVit_a_iu() {
        return vit_a_iu;
    }

    public void setVit_a_iu(String vit_a_iu) {
        this.vit_a_iu = vit_a_iu;
    }

    public String getVit_c() {
        return vit_c;
    }

    public void setVit_c(String vit_c) {
        this.vit_c = vit_c;
    }
}



Are you asking how to Write Data To Realm?

I want to do both writing and reading operations… but this training was not enough for me.

@Mehmet_Deneme: May I know what is the purpose of the Volley library in the current context? Also, you can check this example as a reference to read & write data.

Hello, there is a JSON structure, as I mentioned earlier in the code at the top. My data is stored in it and with the help of Volley library, I pull this JSON file from the web and parsing it. (I just started using Retrofit) I’m developing with Java.

It was difficult to create non-static classes to pull nested data in Realm. I couldn’t find any examples (like my data?) specifically regarding array in object.

@Mehmet_Deneme: Thanks for the context, I would suggest in this case you can simply extend your model classes with RealmObject, and then you can save the object directly with the help of Realm.

Also for array, you can use RealmList and checkout this example which has a more complex implementation and also have a few examples of List/Array.

I created a small project here. It is really difficult to make such an application with SQLite and it is very poor in terms of performance… I am also stuck between Realm and Room. I want to use it for many years. I need a database that is fast reliable and can store large data. (Java)

@Mehmet_Deneme : I would recommend you to give Realm a try. Also, in repo do you want to me check anything, as all I could see is Room implementation.

Finally, I was able to solve the problem of saving and extracting data in Realm. It was really simple… but I still have my doubts. In the forums I read, everyone says that I should use Room (I didn’t do it in Room) because Room is the core architecture, so there will be no errors, and it performs updating and deleting processes faster than Realm. I am really confused…

Can you help with the Room? I want to test it too

@Mehmet_Deneme: Can you please point me to the area you feel Room is faster so I can have a look around it? Also, I don’t have expertise with Room so can’t help much :slightly_frowning_face: