Search code examples
androidjsonandroid-api-levels

Why Json Object order mixed up in android KITKAT and below versions?


I am trying to send these JSONbject to server for login and its working like a charm in android lollipop and marshmallow but when I trying to login in android kitkat and below version then its giving incorrect username and password error because of json order mixup. How can I solve that ?

       JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("USERName", username);
                jsonObject.put("LOGINPASSWORD", password);
                jsonObject.put("IMEINUMBER1", imeino);
                jsonObject.put("Latitude", latitude);
                jsonObject.put("Longitude", longitude);
            } catch (JSONException e) {
                e.printStackTrace();
            }

Below is my jsonObject which mixup in Android Kitkat and below versions.

{"USERName":"Rahul","Latitude":24.588532515497256,"Longitude":73.7020509167292,"IMEINUMBER1":"911375058484548","LOGINPASSWORD":"12345"}

Solution

  • a JSONObject doesn't guarantee any order for its keys, it may sometimes be in the order of insertion, and sometimes not.

    If you need to keep the order of insertion, use a JSONArray instead.