2011-08-19 66 views
3

我有一個對象的列表,像如下如何使用flex json序列化對象列表?

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

用於序列化對象到字符串我這樣做,但它不工作....我缺少somet興...

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

我需要什麼序列化對象到字符串?

+0

你嘗試過什麼?請張貼一些代碼,在完成你的目標展示你的最好的嘗試,並要求有關引起錯誤或給你的麻煩... – maerics

+0

@maerics一個部分一個具體問題:做ü有關於flexjson任何想法.. –

回答

1

我從來沒有與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; } 
    } 
} 

似乎爲我工作。

+0

嘗試在產品名字像一些字符串「這是(腐)/牛奶,我昨天剛喝了」 –

+0

是啊,仍然能正常使用我的JDK 1.6.0_26和flexjson-2.1。 – maerics

+0

我使用flexjson1.9.2 –

1

不幸的是,如果你需要反序列化JSON到一個集合類屬性必須被包括在內。在上面的例子中,JSON字符串被反序列化如下:

List<ProductInfo> ls = new JSONDeserializer<List<ProductInfo>>().deserialize(s);  
System.out.println(ls);  
// => [{name=Stack, productId=1}, {name=Overflow, productId=2}] 

如果你要嘗試直接訪問一個元素,可以說ls.get(0) y您會收到一個ClassCastException: java.util.HashMap cannot be cast to ProductInfo

您必須序列化你的對象,包括以適當的反序列化到一個集合類屬性。

3
List<ProductInfo> ls = new JSONDeserializer<ArrayList<ProductInfo>>().use("values", ProductInfo.class).deserialize(s); 

關注這個link或讀護理完全以下的地圖和集合

重構的路徑列表。在之前的版本中,沒有辦法指定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) 

通過添加「鍵」和「值」的路徑person.friends你可以指定實際的具體類使用Map的鍵和值。對於集合,您可以簡單地附加「值」來指定包含的類。例如:

new JSONDeserializer<List<Person>>().use("people", ArrayList.class).use("people.values", Person.class)