繁体   English   中英

使用Retrofit和GreenDao与嵌套的json对象

[英]Using Retrofit and GreenDao with nested json objects

我想结合使用Retrofit和GreenDao,但我遇到了嵌套Json-Objects的问题。 我的嵌套字段保持为空。

这是Json DataStructure

[
    {
        "id": 1, 
        "street": "Streetname", 
        "zipcode": 12345, 
        "city": "MyCity", 
        "phone_number": "+123456789", 
        "position": "12.0000, 9.0000", 
        "company": {
            "title": "CompanyName", 
            "group": {
                "title": "GroupName"
            }
        }
    }
]

我的DaoGenerator看起来像这样

    Entity customItem = schema.addEntity("CustomItems");
    customItem.addIdProperty();
    customItem.addStringProperty("street");
    customItem.addIntProperty("zipcode");
    customItem.addStringProperty("city");
    customItem.addStringProperty("phone_number");
    customItem.addStringProperty("position");

    Entity company = schema.addEntity("Company");
    company.addIdProperty();
    company.addStringProperty("title");

    Entity group = schema.addEntity("Group");
    group.addIdProperty();
    group.addStringProperty("title");

    Property companyPropId = customItem.addLongProperty("companyId").notNull().getProperty();
    customItem.addToOne(company, companyPropId);

    Property groupPropId = company.addLongProperty("groupId").notNull().getProperty();
    company.addToOne(group, groupPropId);

我的问题是customItem.getCompany()返回null但值“id”到“position”是好的。 我不确定问题是什么,因为我的CustomItem类包含成员

private Company company;

公司的制定者和我看不到任何拼写错误。

public void setCompany(Company company) {
    if (company == null) {
        throw new DaoException("To-one property 'companyId' has not-null constraint; cannot set to-one to null");
    }
    synchronized (this) {
        this.company = company;
        companyId = company.getId();
        company__resolvedKey = companyId;
    }
}

我让它运行但我有多个问题。

1)当我想要持久化CustomItem,Company和Group时,问题是getters getCompany()和getGroup()返回null,因为它们不直接返回成员但是从DB中获取它。 因此,我在生成的CustomItem实体类中添加了一个getter,它只返回公司成员。 现在我能够将公司插入数据库。 getter看起来像这样:

// KEEP METHODS - put your custom methods here
public Company getCompanyLocal() {
    return company;
}
// KEEP METHODS END

同样适用于公司和集团。 但还有另一个问题......

2)第二个问题是实体'Group',因为'group'是一个保留的SQL关键字。 我看到这个问题的一个解决方案和一个不好的解决方法:

  • 好的方法是将json数据从'group'更改为'business_group',并根据更改你的DAO。 完成。

  • 糟糕的解决方法,如果你处于像我这样的情况,你无法改变json,你可以做以下事情。 我根本不坚持该组,但可以通过公司访问它。 它不知何故出现在那里。 因此我在我的Company类中添加了一个getter,就像上面的CustomItem的getter一样。 它有效,但你应该避免这种情况。 因为您无法从数据库查询数据库中的组或加载组。

要解决第二个问题,请将此代码添加到DAO生成器:

beacon.addStringProperty("business_group"); //Daogenerator

并将此代码添加到您的网络管理器中:

//add this into your network manager
FieldNamingStrategy strategy = new FieldNamingStrategy() {
        @Override
        public String translateName(Field field) {
            if(field.getName().equalsIgnoreCase("business_group")) {
                return "group";
            } else {
                return field.getName();
            }
        }
    };

并将此属性设置为您的Gson:

//add this in your Gson
.setFieldNamingStrategy(strategy)

希望它有所帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM