本文介绍了Realm.IO-可以使用createOrUpdateAllFromJson解析JSON数组吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Doctor 类中,我有RealmList- specializationList .

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

专业化

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:

Parsing JSON using createOrUpdateAllFromJson method:

realm.createOrUpdateAllFromJson(Doctor.class, json);

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

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

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

但是specializationList的大小为0.

But specializationList's size is 0.

领域文档:某些JSON API会返回Real尚不支持的原始类型的数组,例如整数或字符串.

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

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

Can JSON array(specializations) be parsed using createOrUpdateAllFromJson?

推荐答案

是的,Realm应该可以解析它,但是看起来您的命名不正确. Doctor类将其称为specializationList,但在JSON中为specializations.

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.

如果您将Doctor类更改为以下类别,则应该可以使用:

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

这篇关于Realm.IO-可以使用createOrUpdateAllFromJson解析JSON数组吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 04:04