2011-02-08 3 views
0

J'essaie d'écrire inner par inner2 puis inner3. Cependant, en ce moment, c'est inner2, inner3 puis inner1. Comment puis-je écrire l'objet JSON pour aller selon la séquence que je veux?Tri séquence JSON

public void toJSON(String description, String Name) 
{ 
    JSONObject inner = new JSONObject(); 
    JSONObject inner2= new JSONObject(); 
    JSONObject inner3= new JSONObject(); 

    try { 
    outer.put("DATA 1", inner); 
    inner.put("description", description); 
    inner.put("filename", imageName); 

    outer.put("DATA 2", inner2);  
    inner2.put("model", "modelname"); 

    outer.put("Datetime", inner3); 
    inner3.put("Datetime", "DateTime"); 

    } catch (JSONException ex) { 
    ex.printStackTrace(); 
    } 
} 
+0

http://stackoverflow.com/questions/979256/how-to-sort-a-json-array – pramodc84

+0

Merci. Mais je suis incapable de l'utiliser. – JohnDoe4136

Répondre

2

De l'JSONObject JavaDocs:

A JSONObject est une collection non ordonnée de paires nom/valeur.

Cela signifie que vous ne pouvez pas avoir de tri personnalisé. Si vous avez besoin de tri sur mesure, envelopper les objets individuels dans un JSONArray:

public void toJSON(String description, String Name){ 
    JSONObject inner = new JSONObject(); 
    JSONObject inner2 = new JSONObject(); 
    JSONObject inner3 = new JSONObject(); 
    JSONArray array = new JSONArray(); 

    try{ 
     array.put(inner); 
     inner.put("description", description); 
     inner.put("filename", imageName); 

     array.put(inner2); 
     inner2.put("model", "modelname"); 

     array.put(inner3); 
     inner3.put("Datetime", "DateTime"); 

     outer.put("values", array); 

    } catch(JSONException ex){ 
     ex.printStackTrace(); 
    } 
}