【发布时间】: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。这样会好一些。但我想做得更好。
【问题讨论】:
-
既然您没有问题,codereview.stackexchange.com 可能更合适。
-
哦,我的错。下次我会在好地方发帖。