我有一个Java类,我想从类的对象生成一个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
对象。我的问题是,它只转换标题和subTitle成员JSON,数组不转换。谁能告诉我如何将数组包含到JSON中?谢谢。
我已经算出来了,下面是正确的函数:
// 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);
}