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?