2016-06-18 24 views
0

這是我的POJO類。UnrecognizedPropertyException:Android中無法識別的字段錯誤

public class Product implements ParentListItem { 
    private String ProductName; 
    private int ProductID; 
    private String ProductImagePath; 
    private String BrandName; 
    private int BrandID; 
    private String SubCategoryName; 
    private int SubCategoryID; 
    private List<ProductVariant> Variants = new ArrayList<>(); 

    Product(){} 
} 

JSON格式:

[{ 
    "Variants": [{ 
    "VariantID": "1", 
    "VariantName": "50 GM", 
    "VariantImagePath": null, 
    "MRP": "19.00", 
    "SellPrice": "18.24", 
    "InCart": "0" 
    }], 
    "ProductName": "Body Cleanser - Lemon Honey Kanti", 
    "ProductID": "1", 
    "BrandName": "Patanjali", 
    "SubCategoryID": "44", 
    "SubCategoryName": "Bathing Soap", 
    "ProductImagePath": "\/images\/patanjali\/1819.png", 
    "BrandID": "112" 
}] 

我試圖用這個POJO這樣。

for (DataSnapshot postSnapshot : snapshot.getChildren()) { 
    Product product = postSnapshot.getValue(Product.class); 
    products.add(product); 
} 

但我得到這個錯誤:

Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "BrandID" (class com.example.sony.models.Product), not marked as ignorable (9 known properties: , "brandID", "subCategoryName", "productID", "childItemList", "variants", "productImagePath", "brandName", "subCategoryID", "productName"])

無法識別的領域 「BrandID」,但這一領域是提供POJO。

我不明白爲什麼我的資本案例字段正在轉換成小寫字母?

爲什麼會出現此錯誤?如何解決這個問題?

+0

[http://stackoverflow.com/questions/4486787/jackson-with-json - 無法識別字段未標記爲可忽略](http://stackoverflow.com/questions/4486787/jackson-with-json-unrecognized-field-not-marked-as-ignorable) –

+0

@ MD,謝謝你響應。但在我的情況下,所有的領域都存在。所以我不認爲我需要將它們標記爲可忽略 –

+0

您的JSON將BrandID作爲字符串,而POJO將其作爲int。你可以嘗試將其更改爲字符串? – basilisk

回答

3

傑克遜反序列化:

  • 公共字段
  • 非公用getter或setter字段