1

I am writing a json file to my external storage. Below code is used to create json objects and then adding them to json array

 JSONObject jsonObj = new JSONObject();

                jsonObj.put("slotid", "1a");
                jsonObj.put("updatetime", 1477650798);
                jsonObj.put("active", true);
                jsonObj.put("adurl", "httpAd");
                jsonObj.put("imgurl", "httpImg");
                jsonObj.put("hd", false);
                jsonObj.put("x", 0);
                jsonObj.put("y", 0);

                phoneArray.put(jsonObj);
                jsobObj.put("slots", phoneArray);

Above code works fine for API24 but when i try to run same code for API 19 then following output occurs

{
  "containers": [ ],
  "slots": [
    {
      "slotid": "1a",
      "updatetime": 1477650798,
      "adurl": "httpAd",
      "active": true,
      "hd": false,
      "y": 0,
      "x": 0,
      "imgurl": "httpImg"
    },
    {
      "slotid": "1a",
      "updatetime": 1477650798,
      "adurl": "httpAd",
      "active": true,
      "hd": false,
      "y": 0,
      "x": 0,
      "imgurl": "httpImg"
    }
  ]
}

My question is why my jsonObjects are not writing in order? I am successfully getting correct output on API 24 i.e

{
  "slots": [
    {
      "slotid": "1a",
      "updatetime": 1477650798,
      "active": true,
      "adurl": "httpAd",
      "imgurl": "httpImg",
      "hd": "false",
      "x": 0,
      "y": 0
    },
    {
      "slotid": "1a",
      "updatetime": 1477650798,
      "active": true,
      "adurl": "httpAd",
      "imgurl": "httpImg",
      "hd": "false",
      "x": 0,
      "y": 0
    }
  ],
  "containers": []
}

Where should be the problem?

Iqra Butt
  • 33
  • 6
  • Whatever the ordering may be the working still remains same it will not be affected ... – AgentP May 21 '20 at 10:31
  • I will try But why this is happening? – Iqra Butt May 21 '20 at 10:33
  • There may be various reasons I am not sure exactly ... may be the library first give importance to variables that have values and then variables that dont have any values in order to make easy for developers to look at.. I guess – AgentP May 21 '20 at 10:35
  • but every variable has some value in it – Iqra Butt May 21 '20 at 10:40
  • May be as I said It was a guess the reason I made it was the containers array has zero objects in it and in second output it came after slots so... – AgentP May 21 '20 at 10:41
  • yes but i have no issue with containers, the issue is that the json array i.e "slots" which contains json objects are not writing in order – Iqra Butt May 21 '20 at 10:50
  • May I know is there any problem that are you facing with it ? – AgentP May 21 '20 at 10:51
  • I did not test it but i am actually ordered to do it that way – Iqra Butt May 21 '20 at 10:54
  • Ok ... that's fine let me know if you get any error – AgentP May 21 '20 at 10:55
  • ok mate. one more question that can i break line after putting each json object into json array like above code i.e jsobObj.put("slots", phoneArray);? So that next next objects can be written on next line so that it is easy to view the code – Iqra Butt May 21 '20 at 10:59
  • No actually by default the JSON will try to save you space by removing unwanted space and again it will not affect the working .... but if you want them to have brakes there is a function called setPrettyPrinting() use that it will print with breaks... – AgentP May 21 '20 at 11:01
  • JSONObject in API 19 and below uses HashMap where as from API 20 the same JSONObject uses LinkedHashMap (no official doc found but posts avaliable). There are many posts with similar questions in Stack Overflow for the same, i.e., check the below link https://stackoverflow.com/questions/39346407/why-json-object-order-mixed-up-in-android-kitkat-and-below-versions, if really want different approach to attain the same see this link https://stackoverflow.com/questions/4515676/keep-the-order-of-the-json-keys-during-json-conversion-to-csv – satuser May 21 '20 at 11:52
  • I tested it, it works fine but my requirement is to arrange my JSONObjects in order :( what should i try – Iqra Butt May 21 '20 at 15:30
  • But why do you want them in order? You're not going to read the JSON obviously, you'll parse it using GSON/Moshi or anythinng else and that way, order of the elements doesn't matter. – Lalit Fauzdar May 22 '20 at 03:57
  • yes you are right, i am getting all i want but i have got a job to do that and i have asked to order my jsonObjects in anyway. It sucks but i have to :( – Iqra Butt May 22 '20 at 08:32

0 Answers0