java - How to preserve LinkedHashMap ordering in JSONObject? -
i have linkedhashmap states.
map<string, string> statemap = new linkedhashmap<string, string>(); // ... i'm creating jsonobject based on it.
jsonobject json = new jsonobject(); json.putall(statemap); however, entries appear unordered. i'd preserve ordering of linkedhashmap in jsonobject. how can achieve this?
unlike jsonobject, jsonarray ordered sequence of values. if want preserve order of map, can construct json object 2 keys:
the first key can called data , hold
statemapdata :json.element('data', statemap)the second key can called keys , jsonarray object hold ordered list of map keys :
jsonarray array = new jsonarray(); array.addall(statemap.keyset()) json.put('keys', array)
for more information :
Comments
Post a Comment