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

随声附和 提交于 2019-12-11 03:47:40

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/30234343/realm-io-can-json-array-be-parsed-using-createorupdateallfromjson

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!