2013-05-06 32 views
0

我有一個Java類,我想從類的對象生成一個JSON字符串。然而,是類的成員如下:Java flexjson序列化組成的JSON對象

/** 
* The set containing the footer texts 
*/ 
public HeaderOrFooter[] footers = null; 
/** 
* The title of the word cloud 
*/ 
public HeaderOrFooter title; 
/** 
* The subtitle of the word cloud 
*/ 
public HeaderOrFooter subTitle; 
/** 
* The set of rectangles to be drawn 
*/ 
public Rectangle[] rectangles; 
/** 
* The set of round rectangles to be drawn 
*/ 
public RoundRectangle[] roundRectangles; 
/** 
* The set of lines to be drawn 
*/ 
public Line[] lines; 
/** 
* The set of polygons to be drawn 
*/ 
public Polygon[] polygons; 
/** 
* The set of words to be drawn 
*/ 
public Word[] words; 

和我的方法,其將該對象JSON是這樣的:

public String convertToJSON() 
{ 
    flexjson.JSONSerializer jsonSerializer = new flexjson.JSONSerializer(); 
    jsonSerializer.exclude("class"); 
    jsonSerializer.exclude("subTitle.class"); 
    jsonSerializer.exclude("title.class"); 
    return jsonSerializer.serialize(this); 
} 

我使用flexjson及其JSONSerializer對象。我的問題是,它只將標題和子標題成員轉換爲JSON,數組不轉換。有人能告訴我如何將數組包括到我的JSON中?謝謝。

+0

你給了整個陣列,'新線[1]'或者類似的東西值?之後,你填寫'Line'對象。 – 2013-05-06 22:36:46

+0

陣列充滿了價值。我可以分別序列化它們,但是我想從包含它們的對象中生成JSON。 – 2013-05-07 00:01:09

回答

3

我已經想通了,在這裏它是正確的功能:

// Re-using the serializer as per "Thread Safety and Reuse" 
// section of http://flexjson.sourceforge.net/ 
public static final flexjson.JSONSerializer jsonSerializer; 
static { 
    jsonSerializer = new flexjson.JSONSerializer().exclude("*.class"); 
} 

public String toJSON() { 
    return jsonSerializer.deepSerialize(this); 
}