1

I am using JSONObject for entering data and computing something out if it. Lets suppose i entered data in form :-

JSONObject obj = new JSONObject();
obj.put("uuid", newImageUuid);
obj.put("type", "image");
obj.put("value", base64ImagePath);

For SDK version above 19 i get it in perfect order but for versions below it the order of the json reverses. Value comes first , then type, then uuid.

I have tried this way :-

Map obj=new LinkedHashMap();
                obj.put("uuid", newImageUuid);
                obj.put("type", "image");
                obj.put("value", base64ImagePath);
                StringWriter data = new StringWriter();
                JSONValue.writeJSONString(obj, data);

Data is in ordered manner and in the form in which i want but i cant make use of this data as type of it is StringWriter. But when i convert this data back into JSONObject , result will be in unordered fashion . I have tried many methods like storing in linkedhashmaps and then converting into JSONObjects but of no use. Could anyone please help to it.

ritwik
  • 101
  • 1
  • 11

2 Answers2

0

As JSON official doc says:

An object is an unordered set of name/value pairs.

So JSONObject does't have any specific order.. Neither Serializer nor Parsers doesn't care about order of JSONObject key/values. Now if you really insist in keeping order you can try to convert your Key/Values to Array of Objects composited of Key and Value.

Community
  • 1
  • 1
Keivan Esbati
  • 3,376
  • 1
  • 22
  • 36
0

JSON Object does not have any order.It is a key/value pair.We can save value with the key

We can get the value by using key

If you want ordered data, you can use array,arraylist

prakash
  • 109
  • 12