2013-05-06 55 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); 
}