我有一个像这样的
的网络服务的json响应{
"id":1,
"name":"abc",
"address": {
"streetName":"cde",
"city":NY
}
}
我正在为我的项目使用Retrofit和greenDao。对于两者,我们需要一个数据模型。仅对于改造,我的数据模型看起来像这样
public class Example {
private Integer id;
private String name;
private Address address;
public Example() {
}
public Example(Integer id, String name, Address address) {
this.id = id;
this.name = name;
this.address = address;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
public class Address {
private String streetName;
private String city;
public Address() {
}
public Address(String streetName, String city) {
this.streetName = streetName;
this.city = city;
}
public String getStreetName() {
return streetName;
}
public void setStreetName(String streetName) {
this.streetName = streetName;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
哪个可用于Retrofit
,但greenDao
有一个生成器,它也可以生成数据模型。如何从greenDao生成器项目生成Datamodel,可以用于改造和greenDao
提前致谢
答案 0 :(得分:2)
以下是生成器的外观:
Schema schema = new Schema(1, <your.package.id.where.models.are.held>);
Entity model = schema.addEntity("Model");
model.addIdProperty();
model.addStringProperty("name");
Entity address = schema.addEntity("Address");
Property addressIdProperty = address.addIdProperty().getProperty();
address.addStringProperty("streetName");
address.addStringProperty("city");
model.addToOne(address, addressIdProperty).setName("address");
DaoGenerator generator = new DaoGenerator();
generator.generateAll(schema, "./app/src/main/java");
如您所见,Model
类包含对地址的引用,通过添加一对一关系并将其命名为“address”。
这样任何像GSON这样的json解析器都可以解析你的对象并将它们放在数据库中。