Realm.IO - 可以使用createOrUpdateAllFromJson解析JSON数组吗?

时间:2015-05-14 09:57:26

标签: android json realm

Doctor 类中,我有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;
....

专业化

public class Specialization extends RealmObject{

    private String specializationName;
...

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"
  }
]

使用 createOrUpdateAllFromJson 方法解析JSON:

realm.createOrUpdateAllFromJson(Doctor.class, json);

我要做的是从医生对象获取RealmList:

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

但是specializationList的大小是0.

领域文档: 一些JSON API将返回原始类型的数组,如整数或字符串,Realm尚不支持。

可以使用createOrUpdateAllFromJson解析JSON数组(特化)吗?

1 个答案:

答案 0 :(得分:3)

是的,Realm应该能够解析它,但看起来你的命名不对。您的Doctor类将其称为specializationList,但在您的JSON中,它是specializations

如果您将Doctor类更改为以下内容,则应该有效:

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