【问题标题】:Java flexjson Serializing composed JSON objectsJava flexjson 序列化组合的 JSON 对象
【发布时间】:2013-05-06 22:30:28
【问题描述】:

我有一个 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 对象。我的问题是它只将标题和子标题成员转换为 JSON,数组没有转换。有人可以告诉我如何将数组包含到我的 JSON 中吗?谢谢。

【问题讨论】:

  • 您是否为数组指定了 new Line[1] 或类似的值?之后填写Line 对象。
  • 数组填充了值。我可以单独序列化它们,但我想从包含它们的对象生成 JSON。

标签: java json serialization flexjson


【解决方案1】:

我想通了,这里是正确的函数:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    • 1970-01-01
    相关资源
    最近更新 更多