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

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

Doctor class 里面,我有 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;
....

专业化 class

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

我想做的是从 doctor object:

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

但是 specializationList 的大小为 0。

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

可以使用 createOrUpdateAllFromJson 解析 JSON array(specializations) 吗?

是的,Realm 应该能够解析它,但看起来你的命名不正确。你的医生 class 称它为 specializationList 但在你的 JSON 中它是 specializations

如果您将 Doctor class 更改为以下内容,它应该会起作用:

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