我需要解析从外部系统传入的json。 这里是json的例子:
{
"address": "My address",
"contacts": "My contacts",
"exchangeRatesMap": {
"CHF": {
"field1": "7.103",
"field2": "11.003"
},
"EUR": {
"field1": "90.123",
"field2": "1.003"
},
"USD": {
"field1": "0.103",
"field2": "20.123"
}
},
"name": "Name of organization",
"id": 1
}
结果我在我的Android应用程序中创建了Java POJO(不是Realm):
public class ExchangeRate {
private int id;
private String name;
private String address;
private String contacts;
private Map<String, Map<String, String>> exchangeRatesMap = new HashMap<String, Map<String, String>>();
}
它的工作正常。行。
现在我需要将Java POJO迁移到Realm Object。
问题: 对象 ExchangeRates 如何看作 RealmObject ?
答案 0 :(得分:1)
Realm尚不支持地图(请参阅https://github.com/realm/realm-java/issues/759),但您可以通过以下方式手动将其转换为List:
public class ExchangeRateItem extends RealmObject {
public String key;
public double field1;
public double field2;
}
public class ExchangeRate {
private int id;
private String name;
private String address;
private String contacts;
private RealmList<ExchangeRateItem> exchangeRates;
}
您需要找到一种方法将ExchangeRate块转换为一个对象。具体取决于您使用的JSON反序列化器类型或直接使用JSON。