Search code examples
javaandroidrealm

Is there a way that realm createOrUpdateAllFromJson() could read nested JSONArrays and create inner RealmObjects?


I have the following JSON modelling groups with students:

"groups":[{
    "id": "1",
    "grade":"second",
    "group": "A",
    "students":[{
      "id": "1",
      "address": "[email protected]",
      "name": "Geoffrey Guthrie",
      "phone": "475690",
      "parent": "Ronald Zhang",
      "diagnosis": "Whitehead",
    },        
    ...}],
...}]

and their respective classes are as follows (setters and getters not included):

public class Student extends RealmObject {
   private String id;
   private String name;
   private String address;
   private String phone;
   private String parent;
   private String diagnosis;
}

and:

public class Group extends RealmObject{
    private String id;   
    private String grade;    
    private String group;
    private RealmList<Student> students;
}

My question here is if the method createOrUpdateAllFromJson, as stated above, supports those nested arrays or should I do it manually getting the arrays inside and then call the same function to create the list of students for each group?


Solution

  • No you don't need to manually getting the arrays inside and then call the same function to create the list of students for each group. But you will not be able to use

    createOrUpdateAllFromJson() for JOSNArray

    or

    createOrUpdateObjectFromJson() for JSONObject

    Because createOrUpdate requires a primary key. But according to your Json structure there is no primary key for the root object. My code blocks will describe the scenario.

    Your Json Structure-

    {
    "groups":[{
    "id": "1",
    "grade":"second",
    "group": "A",
    "students":[{
      "id": "1",
      "address": "[email protected]",
      "name": "Geoffrey Guthrie",
      "phone": "475690",
      "parent": "Ronald Zhang",
      "diagnosis": "Whitehead"
      }]
     }]
    }
    

    According this Your Models should be as follows-

    public class Students extends RealmObject {
        private String id;
        private String name;
        private String address;
        private String phone;
        private String parent;
        private String diagnosis;
    }
    

    and:

    public class Groups extends RealmObject {
        private String id;
        private String grade;
        private String group;
        private RealmList<Students> students;
    }
    

    and:

    public class BaseObject extends RealmObject {
        private RealmList<Groups> groups;
    }
    

    And then your transaction will be like this-

    realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                realm.createObjectFromJson(BaseObject.class, json);
            }
        });
    

    You can add @PrimaryKey annotation for Groups and Students id to avoid duplicate entry if necessary. Hope this structure will reduce your hassle.