如何使用flex json序列化对象列表? [英] How can I serialize a list of objects using flex json?

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

问题描述

我有一个如下对象的列表

I have a list of an object like follows

 List<ProductInfo> 

我想使用flex json对其进行序列化,以便它应该像

I want to serialize it using flex json so that it should like

 [{"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?

推荐答案

我以前从未玩过 flexjson 但在下载并玩它之后我就会想到:

I've never played with flexjson before but after downloading it and playing with it here is what I've come up with:

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

似乎对我有用。

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

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