-4

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"}
Burhanuddin Rashid
  • 5,260
  • 6
  • 34
  • 51
Noreen Khan
  • 233
  • 3
  • 14
  • 1
    json is unordered ! Check [here](http://androiddhina.blogspot.in/2015/09/ordered-json-string-in-android.html) – Piyush Sep 06 '16 at 10:15
  • Its working correct in Android Lollipop and marshmallow. But for below android version why its become unordered ? – Noreen Khan Sep 06 '16 at 10:18
  • You don't understood - you can't make things work over standards. `JSON` standard saying that JSON object is **an unordered set of name/value pairs** that mean that reader should never depends on JSON fields ordering but rather access values by `name`. If you want to preserve order use JSON arrays instead: **An array is an ordered sequence of zero or more values.** – j2ko Sep 06 '16 at 10:23

1 Answers1

0

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.

marmor
  • 27,641
  • 11
  • 107
  • 150