如何使用Flexjson JSONDeserializer? [英] How to use Flexjson JSONDeserializer?

查看:570
本文介绍了如何使用Flexjson JSONDeserializer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串:

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

如何使用Flexjson的JSONDeserializer从上面的字符串中获取所有 product_id

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

我有一个名为<的类code> productinformation 其中包含 product_id name 等字段。

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

推荐答案

您可以使用 JSONDeserializer.use()方法告诉它如何反序列化数组和数组中的每个对象,在本例中为类 ProductInformation 。 product_id属性与flexjson期望的标准命名不匹配,因此对象上的属性需要在其中包含下划线。

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

反序列化部分中的不带训练轮的反序列化部分如果类型信息未包含在JSON字符串中,则docs 会考虑其他案例的其他详细信息。

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.

这篇关于如何使用Flexjson JSONDeserializer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆