2

I am using an Android App to connect to a webservice to store CONTACT details. JSON request data is basically 5 CONTACT details(Name & Phone Number) along with an accessToken(used to authenticate the user).

I am putting the key-value pairs of the JSON object sequentially like I want it to be generated but when I am printing the data on the console the sequence is not maintained.

HOW I WANT THE JSON TO BE GENERATED

{
    "contact_1": {
        "name": "Person 1",
        "contact_num": "001"
    },
    "contact_2": {
        "name": "Person 2",
        "contact_num": "002"
    },
    "contact_3": {
        "name": "Person 3",
        "contact_num": "003"
    },
    "contact_4": {
        "name": "Person 4",
        "contact_num": "004"
    },
    "contact_5": {
        "name": "Person 5",
        "contact_num": "005"
    },
    "accessToken": "9n5h4jbtXFDl4"
}

HOW THE JSON IS ACTUALLY GENEREATED

{
    "contact_4": {
        "contact_num": "004",
        "name": "Person 4"
    },
    "contact_3": {
        "contact_num": "003",
        "name": "Person 3"
    },
    "contact_5": {
        "contact_num": "005",
        "name": "Person 5"
    },
    "accessToken": "84j48HCgtZ8b8",
    "contact_2": {
        "contact_num": "002",
        "name": "Person 2"
    },
    "contact_1": {
        "contact_num": "001",
        "name": "Person 1"
    }
}

CODE USED FOR GENERATING THE JSON

JSONObject jsonObjectInner = new JSONObject();
            JSONObject jsonObjectOuter = new JSONObject();
            try
            {
                jsonObjectInner.put("name", "Person 1");
                jsonObjectInner.put("contact_num", "001");
                jsonObjectOuter.put("contact_1", jsonObjectInner);


                jsonObjectInner = new JSONObject();
                jsonObjectInner.put("name", "Person 2");
                jsonObjectInner.put("contact_num", "002");
                jsonObjectOuter.put("contact_2", jsonObjectInner);


                jsonObjectInner = new JSONObject();
                jsonObjectInner.put("name", "Person 3");
                jsonObjectInner.put("contact_num", "003");
                jsonObjectOuter.put("contact_3", jsonObjectInner);

                jsonObjectInner = new JSONObject();
                jsonObjectInner.put("name", "Person 4");
                jsonObjectInner.put("contact_num", "004");
                jsonObjectOuter.put("contact_4", jsonObjectInner);


                jsonObjectInner = new JSONObject();
                jsonObjectInner.put("name", "Person 5");
                jsonObjectInner.put("contact_num", "005");
                jsonObjectOuter.put("contact_5", jsonObjectInner);

                accessToken = SettingConnector.readString(context, SettingConnector.ACCESS_TOKEN, null);
                jsonObjectOuter.put("accessToken", accessToken);


                commonAPI.SendRequest(value, "http://sandbox.example.com/setEmergencyNumbers");

Is there any way by which I can maintain the sequence?

tsultan1990
  • 35
  • 1
  • 6

5 Answers5

3

According to the definition:

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

So JSONObject is not required to and does not maintain order.

On the bright side - if you use a JSON library, you don't need the ordering anyway.

MByD
  • 135,866
  • 28
  • 264
  • 277
1

As the other answer says, you should not rely on the order in a JSONObject. But if you want for some reason your data to be ordered, you can use the JSONArray class.

sonic
  • 1,894
  • 1
  • 18
  • 22
0

You cannot and should not rely on the ordering of elements within a JSON object.

From the JSON specification at json.org

An object is an unordered set of name/value pairs

Gustavo Morales
  • 2,614
  • 9
  • 29
  • 37
0

JSON is inherently an unordered list of Key/Values Pairs, as explained here in this answer: [JSONObject : Why JSONObject changing the order of attributes

But it shouldn't matter, you can access it's members by doing

varName.contact_1.name

I used varName because assigning this JSON to a variable will make using it much easier.

If you want to process each element like an array then creat a JSON array by removing your outter{} and replacing them with []

Community
  • 1
  • 1
0

You can store those contacts in a json array. That will maintain order.

Raman Shrivastava
  • 2,923
  • 15
  • 26