gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 08:43:54 24 4
gpt4 key购买 nike

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

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

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

但是 specializationList 的大小为 0。

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

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

最佳答案

是的,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;
....

关于android - Realm.IO - 可以使用 createOrUpdateAllFromJson 解析 JSON 数组吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30234343/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com