java - 如何使用flex json序列化对象列表?

原文 标签 java serialization

How can I serialize a list of objects using flex json?

I have a list of an object like follows

 List<ProductInfo> 

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?

Answer

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

Seems to work for me.

翻译

我有一个对象列表,如下所示

 List<ProductInfo> 


我想使用flex json序列化它,所以它应该喜欢

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


从上面的字符串反序列化为对象列表我正在使用以下代码

 final List<ProductInformation> productInformationList = new JSONDeserializer<List<ProductInformation>>().use(null, ArrayList.class)
            .use("values", ProductInformation.class).deserialize(parameterValue);


用于将对象序列化为字符串,我正在这样做,但是它不起作用...。我缺少了一些东西...

final String serializizedString = new JSONSerializer().serialize(productInformationList);


我需要将对象序列化为字符串吗?
最佳答案
我以前从未玩过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; }
  }
}


似乎为我工作。
相关推荐

java - 为每个用户创建单独的表以存储数据或使用单个表

java - 如何更新2个组合框

java - 如何在Java中定期刷新ZipOutputStream

java - Grails域类上的Java接口

java - 在Eclipse中,如何将匿名接口实现重构为Enum Singleton

java - Java错误:java.io.FileNotFoundException:C:\ Users \ FSSD \ Desktop \ My Test(访问被拒绝)

java - 如何将菜单项保持在文本区域的顶部?

java - 使用与该平面文件的多个连接,通过Internet将30GB的平面文本文件导入本地文件系统?

java - 无法在Android上使用正则表达式实现部分匹配

java - 确定性语言的适用性