Joe Rakhimov
Joe Rakhimov

Reputation: 5083

Realm.IO - Can JSON array be parsed using createOrUpdateAllFromJson?

Inside Doctor class, I have RealmList - specializationList.

public class Doctor extends RealmObject {

    @PrimaryKey
    private String doctorId;
    private FullName fullName;
    private Age age;
    private String organizationId;
    private Position position;
    private String category;
    private String loyalty;
    private RealmList<Specialization> specializationList;
    private Contacts contacts;
    private String key;
....

Specialization class

public class Specialization extends RealmObject{

    private String specializationName;
...

Doctors JSON:

[
  {
    "doctorId": "7d8e72d7-809b-4273-9a3f-fa21718dee7f",
    "doctorFullName": {
      "firstName": "FirstName0",
      "lastName": "LastName0",
      "middleName": "MiddleName0"
    },
    "doctorPosition": {
      "positionName": "PositionName0",
      "department": "Department0"
    },
    "organizationId": "7cfaf5c0-127a-4cfc-b73b-52a35fd02ffd",
    "specializations": [
      {
        "specializationName": "Specialization name 3"
      },
      {
        "specializationName": "Specialization name 2"
      },
      {
        "specializationName": "Specialization name 1"
      }
    ],
    "key": "firstname0 middlename0 lastname0"
  }
]

Parsing JSON using createOrUpdateAllFromJson method:

realm.createOrUpdateAllFromJson(Doctor.class, json);

What I am trying to do is getting RealmList from doctor object:

RealmList<Specialization> specializationList = doctor.getSpecializationList();

But specializationList's size is 0.

Realm documentation: Some JSON APIs will return arrays of primitive types like integers or Strings, which Realm doesn’t support yet.

Can JSON array(specializations) be parsed using createOrUpdateAllFromJson?

Upvotes: 2

Views: 2016

Answers (1)

Christian Melchior
Christian Melchior

Reputation: 20126

Yes, Realm should be able to parse that, but it looks like your naming is not right. Your Doctor class calls it specializationList but in your JSON it is specializations.

If you change your Doctor class to the following it should work:

public class Doctor extends RealmObject {

    @PrimaryKey
    private String doctorId;
    private FullName fullName;
    private Age age;
    private String organizationId;
    private Position position;
    private String category;
    private String loyalty;
    private RealmList<Specialization> specializations;
    private Contacts contacts;
    private String key;
....

Upvotes: 3

Related Questions