【问题标题】:java array with void/methods带有 void/方法的 java 数组
【发布时间】:2015-11-20 07:46:35
【问题描述】:

我的代码看起来很糟糕,我想要做得更好。

if (e == null && !result.isJsonNull() && result.get("code").getAsInt() == 200) {
JsonArray array = result.get("data").getAsJsonObject().get("products").getAsJsonArray();
ProductDAO productDAO = new ProductDAO(getApplicationContext());

for (int i = 0; i < array.size(); i++) {
    Product product = new Product();
    if (!isProduct(array.get(i).getAsJsonObject().get("id").getAsString())) {
        if (!array.get(i).getAsJsonObject().get("_vat_amount").isJsonNull() && !array.get(i).getAsJsonObject().get("_vat_amount").toString().equals("")) {
            product.setVatAmount(array.get(i).getAsJsonObject().get("_vat_amount").getAsDouble());
        }
        if (!array.get(i).getAsJsonObject().get("_vat_rate").isJsonNull() && !array.get(i).getAsJsonObject().get("_vat_rate").toString().equals("")) {
            product.setVatRate(array.get(i).getAsJsonObject().get("_vat_rate").getAsDouble());
        }
        if (!array.get(i).getAsJsonObject().get("_vat_rate_s").isJsonNull() && !array.get(i).getAsJsonObject().get("_vat_rate_s").toString().equals("")) {
            product.setVatRateS(array.get(i).getAsJsonObject().get("_vat_rate_s").getAsString());
        }
        if (!array.get(i).getAsJsonObject().get("brutto_value").isJsonNull() && !array.get(i).getAsJsonObject().get("brutto_value").toString().equals("")) {
            product.setBruttoValue(array.get(i).getAsJsonObject().get("brutto_value").getAsDouble());
        }
        if (!array.get(i).getAsJsonObject().get("count").isJsonNull() && !array.get(i).getAsJsonObject().get("count").toString().equals("")) {
            product.setCount(array.get(i).getAsJsonObject().get("count").getAsDouble());
        }
        if (!array.get(i).getAsJsonObject().get("id").isJsonNull() && !array.get(i).getAsJsonObject().get("id").toString().equals("")) {
            product.setFinettoID(array.get(i).getAsJsonObject().get("id").getAsInt());
        }
        if (!array.get(i).getAsJsonObject().get("lp").isJsonNull() && !array.get(i).getAsJsonObject().get("lp").toString().equals("")) {
            product.setLp(array.get(i).getAsJsonObject().get("lp").getAsInt());
        }
        if (!array.get(i).getAsJsonObject().get("name").isJsonNull() && !array.get(i).getAsJsonObject().get("name").toString().equals("")) {
            product.setName(array.get(i).getAsJsonObject().get("name").getAsString());
        }
        if (!array.get(i).getAsJsonObject().get("netto_value").isJsonNull() && !array.get(i).getAsJsonObject().get("netto_value").toString().equals("")) {
            product.setNettoValue(array.get(i).getAsJsonObject().get("netto_value").getAsDouble());
        }
        if (!array.get(i).getAsJsonObject().get("price").isJsonNull() && !array.get(i).getAsJsonObject().get("price").toString().equals("")) {
            product.setPrice(array.get(i).getAsJsonObject().get("price").getAsDouble());
        }
        if (!array.get(i).getAsJsonObject().get("unit").isJsonNull() && !array.get(i).getAsJsonObject().get("unit").toString().equals("")) {
            product.setUnit(array.get(i).getAsJsonObject().get("unit").getAsString());
        }
        productDAO.save(product);
    }
}

有什么方法可以用 void/methods(setter)创建数组吗?我可以剪一些这个json。这样会好一些。但我想做得更好。

【问题讨论】:

标签: java arrays json void


【解决方案1】:

我会创建一些这样的方法(每种类型一个):

private String getAsString(String key, JsonObject jsonObject) {
    if (jsonObject.get(key).isJsonNull() && !jsonObject.get(key).toString().equals("")) {
        return jsonObject.get(key).getAsString());
    } else {
        return null;
    }
}

现在你可以这样做了:

JsonObject jsonObject = array.get(i).getAsJsonObject();

product.setVatAmount(getAsDouble("_vat_amount", jsonObject));
product.setVatRate(getAsDouble("_vat_rate", jsonObject));
product.setVatRateS(getAsString("_vat_rate_s", jsonObject));

等等。

【讨论】:

    猜你喜欢
    • 2013-12-17
    • 2013-12-22
    • 1970-01-01
    • 2012-12-04
    • 2018-12-04
    • 2021-01-16
    • 1970-01-01
    • 2013-07-17
    • 2012-05-12
    相关资源
    最近更新 更多