How to use Flexjson JSONDeserializer?

↘锁芯ラ 提交于 2019-12-03 22:36:19

问题


I have a string:

 [{"product_id":"2","name":'stack"'},{"product_id":"2","name":"overflow"}]"

How can I use Flexjson's JSONDeserializer to obtain all product_ids from the above string?

I have a class called productinformation which has fields like product_id and name.


回答1:


You could use the JSONDeserializer.use() methods to tell it how to deserialize the array and each object in the array, in this case of class ProductInformation . The product_id attribute does not match up with the standard naming that flexjson expects, so your properties on the object will need to have an underscore in them.

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();
}

The section on "Deserialization Without the Training Wheels" in Deserialization section of the docs goes into additional details on the other cases to consider if the type information is not included in the JSON string.



来源:https://stackoverflow.com/questions/7099803/how-to-use-flexjson-jsondeserializer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!