java 如何使用 flex json 序列化对象列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7122368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I serialize a list of objects using flex json?
提问by saurabh ranu
I have a list of an object like follows
我有一个对象列表,如下所示
List<ProductInfo>
I want to serialize it using flex json so that it should like
我想使用 flex json 序列化它,以便它应该喜欢
[{"product_id":"2","name":'stack"'},{"product_id":"2","name":"overflow"}]"
for deserializing from the above string into a List of Objects I am using the following code
为了将上述字符串反序列化为对象列表,我使用以下代码
final List<ProductInformation> productInformationList = new JSONDeserializer<List<ProductInformation>>().use(null, ArrayList.class)
.use("values", ProductInformation.class).deserialize(parameterValue);
for serializing the object to string I am doing this but it's not working....I am missing something...
将对象序列化为字符串我正在这样做,但它不起作用......我错过了一些东西......
final String serializizedString = new JSONSerializer().serialize(productInformationList);
What do I need to serialize the object into a string?
将对象序列化为字符串需要什么?
采纳答案by maerics
I've never played with flexjsonbefore but after downloading it and playing with it here is what I've come up with:
我以前从未玩过flexjson,但在下载它并在这里玩之后,我想到了:
public class TestFlexJson {
public static void main(String args[]) {
ProductInfo p1 = new ProductInfo(1, "Stack");
ProductInfo p2 = new ProductInfo(2, "Overflow");
List<ProductInfo> infos = Arrays.asList(p1, p2);
String s = new JSONSerializer()
.exclude("*.class", "description")
//.include("productId", "name")
// EDIT: the "include" call is irrelevant for this example.
.serialize(infos);
System.out.println(s);
// => [{"name":"Stack","productId":1},{"name":"Overflow","productId":2}]
List<ProductInfo> ls = new JSONDeserializer<List<ProductInfo>>().deserialize(s);
System.out.println(ls);
// => [{name=Stack, productId=1}, {name=Overflow, productId=2}]
}
public static class ProductInfo {
private int id;
private String name;
private String desc; // Not used, to demonstrate "exclude".
public ProductInfo(int id, String name) {
this.id = id;
this.name = name;
}
public int getProductId() { return this.id; }
public String getName() { return this.name; }
public String getDescription() { return this.desc; }
}
}
Seems to work for me.
似乎对我有用。
回答by Shailendra Patidar
List<ProductInfo> ls = new JSONDeserializer<ArrayList<ProductInfo>>().use("values", ProductInfo.class).deserialize(s);
Follow this linkor read care fully following
按照此链接或仔细阅读以下内容
Refactored path listings for Maps and Collections. In prior versions there was no way to specify both the concrete top of a Collection/Map AND the concrete class contained within. The path language was not verbose enough. Now you can specify both the concrete collection AND the concrete class contained within. if person.friends is a path that points to java.util.Map. For example,
重构地图和集合的路径列表。在之前的版本中,无法同时指定 Collection/Map 的具体顶部和其中包含的具体类。路径语言不够详细。现在您可以指定具体集合和其中包含的具体类。如果 person.friends 是指向 java.util.Map 的路径。例如,
new JSONDeserializer<Person>()
.use( "person.friends", HashMap.class )
.use("person.friends.keys", Relation.class )
.use( "person.friends.values", Person.class )
By adding "keys" and "values" to the path person.friends you can specify the actual concrete classes to use for the keys and values of the Map. For Collections you can simply append "values" to specify the containing class. For example:
通过向路径 person.friends 添加“keys”和“values”,您可以指定用于 Map 的键和值的实际具体类。对于集合,您可以简单地附加“值”来指定包含类。例如:
new JSONDeserializer<List<Person>>().use( "people", ArrayList.class ).use("people.values", Person.class )
回答by Mark B.
Unfortunately, the class property must be included if you need to deserialize your json into a collection. In the above example, the json string was deserialized as follows:
不幸的是,如果您需要将 json 反序列化为集合,则必须包含 class 属性。在上面的例子中,json字符串被反序列化如下:
List<ProductInfo> ls = new JSONDeserializer<List<ProductInfo>>().deserialize(s);
System.out.println(ls);
// => [{name=Stack, productId=1}, {name=Overflow, productId=2}]
If you were to try to access an element directly, lets say ls.get(0)
y you would receive a ClassCastException: java.util.HashMap cannot be cast to ProductInfo
.
如果您尝试直接访问一个元素,假设ls.get(0)
您会收到一个ClassCastException: java.util.HashMap cannot be cast to ProductInfo
.
You must serialize your object to include the class property in order to appropriately deserialize into a collection.
您必须序列化您的对象以包含 class 属性,以便适当地反序列化为一个集合。