首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java flexjson序列化组合JSON对象

Java flexjson序列化组合JSON对象
EN

Stack Overflow用户
提问于 2013-05-07 06:30:29
回答 1查看 2K关注 0票数 0

我有一个Java类,我想从该类的对象生成一个JSON字符串。但是,该类的成员如下所示:

代码语言:javascript
复制
/**
 * 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的方法如下所示:

代码语言:javascript
复制
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中?谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-07 11:18:28

我已经弄清楚了,这是正确的函数:

代码语言:javascript
复制
// 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);
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16408359

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档