【发布时间】:2016-05-02 12:58:09
【问题描述】:
我的 JSON 数据从服务器 api 看起来像这样
{
//...
PredecessorIds:[[1,2][3,4][5]]
//...
}
我可以通过 RealmList<RealmInt> 成功处理 Integer 或 String 的数组,但这次我失败并出现错误,因为 RealmList> 不支持说 "Type parameter 'io.realm.realmList' is not within its bounds...."
对于RealmInt,请参阅this link。
我尝试使用 RealmList<RealmLista> 解决它,其中 RealmLista 从 RealmObject 扩展并有一个 RealmList 像这样
public class RealmLista extends RealmObject {
public RealmList<RealmInt> value;
public RealmLista() {
}
public RealmLista(RealmList<RealmInt> val) {
this.value = val;
}
}
然后创建了一个RealmListaTypeAdapter并将其添加到Gson,但是在反序列化Gson expects an Object (RealmLista) but is found array时,从服务器上显示的数据很明显。
//RealmListAdapter for Gson
@Override
public RealmLista read(JsonReader in) throws IOException {
RealmLista lista = new RealmLista();
Gson gson = new Gson();
//how to read that [[1],[3,4]] int into RealmLista
in.beginArray();
while (in.hasNext()) {
lista.value.add(new RealmInt(in.nextInt()));
}
in.endArray();
return lista;
}
有什么方法可以在保存的同时转换成任意类型的RealmObject来存储一个简单的List<List<Integer>>,List<List<Integer>>很容易被Gson转换。 :-/
【问题讨论】:
-
[如果您想存储它们的数组,这仅适用于领域整数的
RealmList<RealmInteger>](https://gist.github.com/cmelchior/1a97377df0c49cd4fca9) but you do need a new object that stores aRealmList`。
标签: android database realm realm-list