1. prepare a LinkedHashMap object with elements
2. convert it to JSONObject
Example:
Map obj = new LinkedHashMap();
obj.put("a", "String1");
obj.put("b", new Integer(1));
obj.put("c", new Boolean(true));
obj.put("d", "String2");
JSONObject json = new JSONObject(obj);
download this library:
https://github.com/douglascrockford/JSON-java
save all the files in a new package in your project
instead of using org.json.JSONObject use your.package.JSONObject which you added from the downloaded library.
now open JSONObject.java file and change HashMap() to LinkedHashMap() in the constructor
public JSONObject(Map map)
This will make the JSONObject store data in the order you entered the values using put.