Java 排序JSON序列
我试着先写inner2,然后再写inner3。然而,现在它是inner2,inner3,然后是inner1。如何编写JSON对象以按照我想要的顺序运行Java 排序JSON序列,java,json,java-me,Java,Json,Java Me,我试着先写inner2,然后再写inner3。然而,现在它是inner2,inner3,然后是inner1。如何编写JSON对象以按照我想要的顺序运行 public void toJSON(String description, String Name) { JSONObject inner = new JSONObject(); JSONObject inner2= new JSONObject(); JSONObject inner3= new JSONObject(
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();
}
}
从:
JSONObject是无序的
名称/值对的集合
这意味着您不能进行自定义排序。如果确实需要自定义排序,请将单个对象包装到:
谢谢但是我不能用这个。
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();
}
}