You can use the following code to do custom ORDERED serialization and deserialization of JSON Array (This example assumes you are ordering Strings but can be applied to all types):
Serialization
JSONArray params = new JSONArray();
int paramIndex = 0;
for (String currParam : mParams)
{
JSONObject paramObject = new JSONObject();
paramObject.put("index", paramIndex);
paramObject.put("value", currParam);
params.put(paramObject);
++paramIndex;
}
json.put("orderedArray", params);
Deserialization
JSONArray paramsJsonArray = json.optJSONArray("orderedArray");
if (null != paramsJsonArray)
{
ArrayList<String> paramsArr = new ArrayList<>();
for (int i = 0; i < paramsJsonArray.length(); i++)
{
JSONObject param = paramsJsonArray.optJSONObject(i);
if (null != param)
{
int paramIndex = param.optInt("index", -1);
String paramValue = param.optString("value", null);
if (paramIndex > -1 && null != paramValue)
{
paramsArr.add(paramIndex, paramValue);
}
}
}
}