您可以使用JSONDeserializer.use()
方法來告訴它如何反序列化數組中的每個對象,在這種情況下的類ProductInformation
。 product_id屬性與flexjson所期望的標準命名不匹配,因此對象的屬性需要在其中包含下劃線。
String products= "[{\"product_id\": \"123\",\"name\":\"stack\"},{\"product_id\": \"456\",\"name\":\"overflow\"}]";
List<ProductInformation> productInfoList = new JSONDeserializer<List<ProductInformation> >()
.use(null, ArrayList.class)
.use("values",ProductInformation.class)
.deserialize(products);
for(ProductInformation productInformation : productInfoList){
System.out.println(productInformation.getProduct_id();
}
在Deserialization section of the docs「反序列化沒有培訓輪子」上的部分進入對其他案件的更多詳細考慮,如果在JSON字符串中不包含的類型信息。
請原諒我的無知,但是什麼是--- use(「values」,ProductInformation.class)..什麼是「values」... –
「values」是flexjson用來引用成員的特定名稱一個集合,在這個例子中是列表。這基本上是說「這個列表中的每個成員都應該反序列化爲一個'ProductInformation'對象。 –
cool ..感謝您的寶貴意見.. –