這是Firebase中的產品結構。變體未在產品類中從JSON轉換爲對象 - firebase實時數據庫android
{
"Available" : "1",
"BrandID" : "26",
"ProductID" : "244",
"ProductImagePath" : "/images/palmolive/moisturising-deluxe.png",
"ProductName" : "Moisturing Deluxe Shaving Cream",
"SubCategoryID" : "23",
"Variants" : {
"244_26" : {
"Available" : "1",
"MRP" : "55.00",
"SellPrice" : "54.00",
"VariantID" : "26"
},
"244_42" : {
"Available" : "1",
"MRP" : "28.00",
"SellPrice" : "27.00",
"VariantID" : "42"
}
}
}
這些是模型類。
產品類別
@JsonIgnoreProperties(ignoreUnknown = true)
public class Product {
private String ProductID;
private String BrandID;
private String SubCategoryID;
private String ProductName;
private String ProductImagePath;
private List<Variants> variants;
}
變體類
@JsonIgnoreProperties(ignoreUnknown = true)
public class Variants {
private String Available;
private String MRP;
private String SellPrice;
private String VariantID;
}
這是用於將JSON成對象
productsRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Map<String, Product> products = new ConcurrentHashMap<>();
products.clear();
if (Utility.getProductMap().size() == 0) {
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
Product product = dataSnapshot1.getValue(Product.class);
products.put(product.getProductID(), product);
}
Utility.setProductMap(products);
}
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w(TAG, "Failed to read value.", error.toException());
Utility.displayToast("Failed to read value." + error.toException());
}
});
問題是變體沒有得到轉化爲對象的邏輯。這個檢查是否(product.getVariants()== null)總是返回true。
這個邏輯有什麼問題?
感謝您的回覆。我改正了這個名字。你能否請幫助我在上課的JSON數據格式中做什麼其他更改以正確解析。 –
編輯,嘗試它,並告訴我,如果仍然有問題,希望不是:) –
我沒有控制來更改JSON數據格式。我只能在課堂上做出改變。在課堂上需要做什麼修改才能解析它? –