我正在将领域与 gson 一起使用。我有一个模态,其中有一个 int 类型字段列表。 Realm 目前不支持基元列表。为了解决这个问题,有一个解决方案。我创建了 RealmInt 类。
import io.realm.RealmObject;
public class RealmInt extends RealmObject {
private int val;
public int getVal() {
return val;
}
public void setVal(int val) {
this.val = val;
}
}
我有一个像这样的大模态对象..
public class Product extends RealmObject {
@PrimaryKey
private int productID;
private int priority;
private boolean isFavourite;
.....
.....
.....
private RealmList<Document> documents;
private RealmList<ProductInfoGroup> productInfoGroups;
private RealmList<RealmInt> categories;
我必须将下面的 json 数组反序列化为产品模态。
[{
"productID": 776,
"categories": [
35
],
"name": "",
"priority": 3,
......
"status": 2,
"documents": [
{
"documentID": 74,
"productID": 776,
"name": null,
....
"isDefault": true
}
],
"productInfoGroups": [
{
"productInfoGroupID": 1575,
"productID": 776,
.....
"productInfos": [
{
"productInfoID": 2707,
"productInfoGroupID": 1575,
"title": "",
...
},
{
"productInfoID": 2708,
"productInfoGroupID": 1575,
...
},
{
"productInfoID": 2709,
.....
}
]
}
],
"lastUpdateDate": 130644319676570000,
"isActive": true
},....]
有一个解决方案here https://stackoverflow.com/questions/28733024/gson-deserialization-of-liststring-into-realmlistrealmstring但它不适用于大物体。我只需要更改类别数组,其他反序列化必须默认完成 gson 反序列化。