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 statemap data :

    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

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -