0

I want to create a JSonObject with some values for call a webservice but where webservice in a order like:

{
    "id" : 1
    "email" : "test@test.com",
    "pin" : 1234,
    "age" : 25,
    "firstName" : "Test First Name",
    "lastName" : "Test Last Name",
    "location" : "India",
    "phone" : "1234567890"
}

but when I create a json object from android code it is not maintaining the order like:

requestJOB=new JSONObject();
requestJOB.put("userid",Pref.getValue(this, Const.USER_ID, requestJOB.optString("userid")));
requestJOB.put("email", Pref.getValue(this, Const.PREF_EMAIL, requestJOB.optString("email")));
requestJOB.put("pin", Pref.getValue(this, Const.PREF_PIN, requestJOB.optString("pin")));
requestJOB.put("age", Pref.getValue(this, Const.PREF_AGE, requestJOB.optString("age")));
requestJOB.put("firstname", etFirstName.getText().toString().trim());
requestJOB.put("lastname", etLastName.getText().toString().trim());
requestJOB.put("phone", etPhone.getText().toString().trim());
requestJOB.put("location", etLocation.getText().toString().trim());

I write the code my desired order but JsonObject change the order in run time. I also tried with map and LinkedList but A exception is occured when I want to convert LIST to JsonObject. I searched in stackoverflow where no satisfactory answer. In this situation I don't understand exactly what I have to do.

Ali Ahmed
  • 1,159
  • 1
  • 8
  • 18
  • 1
    Order doesn't matter. It's in the JSON specifications. See http://stackoverflow.com/questions/16870416/does-the-sequence-of-the-values-matter-in-a-json-object – Andreas Feb 26 '16 at 20:19
  • Use [gson](http://google-gson.googlecode.com/svn/tags/1.2.3/docs/javadocs/com/google/gson/JsonObject.html). The member elements of this object are maintained in order they were added. – Ashraful Islam Feb 26 '16 at 20:20

1 Answers1

1

In Android platform there is better way to serialize a object in json by using Google GSON API... Which provide all possible functionality to convert a class to their corresponding JSON. U can prepare nested jsonobject ..

Nested like json object with in a json object. Json array embedded within a json. Object Multiple jsonarray with in a same json object. And their may be multiple variety .. Just explore this jar .. It's very easy to use and user-friendly jar. Just go and grab it .. Hopefully u feel better with this API

I used this jar in my Android project actually

Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52