How to preserve LinkedHashMap ordering in JSONObject?
Unlike JSONObject
, JSONArray
is an ordered sequence of values.So if you want to preserve the order of your map, you can construct your json
object with two keys:
The first key can be called data and will hold your
stateMap
data like this :json.element('data', stateMap)
The second key can be called keys and it will be a JSONArray object which will hold the ordered list of your map keys like this :
JSONArray array = new JSONArray();array.addAll(stateMap.keySet())json.put('keys', array)
For more information :