Domanda

Come posso concatenare più oggetto JsonRepresentation in uno, senza costruire il mio parser stringa?

Dire che ho due oggetti JsonRepresentation

obj1 = {"name":"obj1"};
obj2 = {"name":"obj2"};

Vorrei ottenere la concatenazione risultato come:

 {
    {"name":"obj1"},
    {"name":"obj2"}
 } 

La lettura del JsonRepresentation, non esiste un modo semplice per farlo se non per fare qualche manipolazione di stringhe. Ho ragione?

Grazie

È stato utile?

Soluzione

Se ti riferisci a questo JsonRepresentation classe , e si desidera unire i 2 oggetti in una matrice, allora si dovrebbe essere in grado di farlo nel seguente modo:

JSONObject jsonObj1 = obj1.toJsonObject();
JSONObject jsonObj2 = obj2.toJsonObject();
JSONArray jsonArray = new JSONArray().append(jsonObj1).append(jsonObj2);
JsonRepresentation jsonConcat = new JsonRepresentation(jsonArray);

. Nota: non ho effettivamente utilizzato la libreria, ma se si comporta per l'API, questo dovrebbe essere abbastanza semplice

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top