这样任何json解析器,如GSON可以解析你的对象,并把它们放在数据库中。
我有一个来自web服务的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生成器项目中生成数据模型,它可以用于改造和greenDao
Thanks in advance
生成器应该是这样的:
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",包含了对address的引用。