For Java code, Create a POJO class for your object instead of a JSONObject.and use JSONEncapsulator for your POJO class.that way order of elements depends on the order of getter setters in your POJO class.for eg. POJO class will be like
Class myObj{String userID;String amount;String success;// getter setters in any order that you want
and where you need to send your json object in response
JSONContentEncapsulator<myObj> JSONObject = new JSONEncapsulator<myObj>("myObject");JSONObject.setObject(myObj);return Response.status(Status.OK).entity(JSONObject).build();
The response of this line will be
{myObject : {//attributes order same as getter setter order.}}