是否可以使用相同模型使用 JSONObject 和 RealmObject 进行解析?
Is it possible to parse with JSONObject and RealmObject with the same model?
我不想为 json 和领域创建单独的模型。我正在寻找一种方法来做到这一点。
如何在不创建两个模型的情况下在单个模型中处理此问题?
我的Json;
"story": {
"id": 3,
"title": "title",
"is_new": false,
"thumbnail": "url",
"original": "url",
}
MyRealmObject
public class stories extends RealmObject {
@PrimaryKey
@Required
private String id;
@Required
private String title;
private boolean isNew;
@Required
private String thumbnail;
@Required
private String original;
[..and getter setter..]
}
您可以对 JSON 解析和 Realm 使用相同的模型。
您可能需要使用 SerializedName
,因为字段 is_new
不起作用。
示例:
public class Stories extend RealmObject {
private int id;
private String title;
@SerializedName("is_new") // required
private Boolean isNew;// use preferred name
private String thumbnail;
private String original;
/* getter & setter */
}
Parsing
Stories mDataClass = new Gson().fromJson("{/*whatever your json object*/}", Stories.class);
我不想为 json 和领域创建单独的模型。我正在寻找一种方法来做到这一点。
如何在不创建两个模型的情况下在单个模型中处理此问题?
我的Json;
"story": {
"id": 3,
"title": "title",
"is_new": false,
"thumbnail": "url",
"original": "url",
}
MyRealmObject
public class stories extends RealmObject {
@PrimaryKey
@Required
private String id;
@Required
private String title;
private boolean isNew;
@Required
private String thumbnail;
@Required
private String original;
[..and getter setter..]
}
您可以对 JSON 解析和 Realm 使用相同的模型。
您可能需要使用 SerializedName
,因为字段 is_new
不起作用。
示例:
public class Stories extend RealmObject {
private int id;
private String title;
@SerializedName("is_new") // required
private Boolean isNew;// use preferred name
private String thumbnail;
private String original;
/* getter & setter */
}
Parsing
Stories mDataClass = new Gson().fromJson("{/*whatever your json object*/}", Stories.class);