开发者

Sorting JSON sequence

开发者 https://www.devze.com 2023-02-09 08:42 出处:网络
I am trying to write inner follow by inner2 then inner3. However, right now it is inner2, inner3 then inner1. How can I write JSON object to go according to the sequence i want?

I am trying to write inner follow by inner2 then inner3. However, right now it is inner2, inner3 then inner1. How can I write JSON object to go according to the sequence i want?

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

    try {   
    out开发者_高级运维er.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();
    }
}


From the JSONObject JavaDocs:

A JSONObject is an unordered collection of name/value pairs.

This means you can't have custom sorting. If you do need custom sorting, wrap the individual objects in a 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();
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号