2013-05-06 3 views
0

J'ai une classe Java et je souhaite générer une chaîne JSON à partir d'un objet de la classe. Cependant, les membres de la classe sont les suivantes:Java flexjson Sérialisation d'objets JSON composés

/** 
* 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; 

et ma méthode qui convertit l'objet JSON ressemble à ceci:

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

J'utilise flexjson et son objet JSONSerializer. Mon problème est qu'il ne convertit que les membres title et subTitle en JSON, les tableaux ne sont pas convertis. Quelqu'un peut-il me dire comment pourrais-je inclure les tableaux à mon JSON? Merci.

+0

Avez-vous donné les tableaux avec une valeur 'nouvelle ligne [1]' ou quelque chose de similaire? Après cela, vous remplissez le (s) objet (s) 'Line'. –

+0

Les tableaux sont remplis de valeur. Je peux les sérialiser séparément, mais je voudrais générer le JSON de mon objet les contenant. –

Répondre

3

Je l'ai pensé à elle, voilà la fonction correcte:

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