Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为greenDao创建通用数据模型并进行改造_Android_Json_Retrofit_Greendao - Fatal编程技术网

Android 为greenDao创建通用数据模型并进行改造

Android 为greenDao创建通用数据模型并进行改造,android,json,retrofit,greendao,Android,Json,Retrofit,Greendao,我有一个来自web服务的json响应,如下所示 { "id":1, "name":"abc", "address": { "streetName":"cde", "city":NY } } 我正在使用改造和绿刀为我的项目。对于两者,我们都需要一个数据模型。仅用于改装,我的数据模型如下所示 public class Example { private Integer id; private

我有一个来自web服务的json响应,如下所示

{
 "id":1,
 "name":"abc",
 "address": {
               "streetName":"cde",
               "city":NY
            }
}
我正在使用改造和绿刀为我的项目。对于两者,我们都需要一个数据模型。仅用于改装,我的数据模型如下所示

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;
    }

}
它可以用于
改造
,但
绿道
有一个生成器,它也可以制作数据模型。如何从greenDao generator项目生成数据模型,该项目可用于改造和greenDao


提前感谢

以下是发电机的外观:

    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");
Schema模式=新模式(1,);
实体模型=schema.addEntity(“模型”);
model.addIdProperty();
model.addStringProperty(“名称”);
实体地址=schema.addEntity(“地址”);
属性addressIdProperty=address.addIdProperty().getProperty();
地址:addStringProperty(“streetName”);
地址:addStringProperty(“城市”);
model.addToOne(地址,addressIdProperty).setName(“地址”);
DaoGenerator=新的DaoGenerator();
generator.generateAll(模式,“./app/src/main/java”);
如您所见,
Model
类通过添加一对一关系并将其命名为“address”,包含对address的引用

这样,任何类似json的解析器都可以解析对象并将它们放入数据库中