String ordered by JSON String from LinkedHashMap

I needed to have Key / Value pairs in the order of my insertion, so I decided to use LinkedHashMap over HashMap . But I need to convert LinkedHashMap to a JSON string, where the order in LinkedHashMap maintained in the string.

But currently I am achieving this:

  • First conversion of LinkedHashMap to JSON.
  • Then convert JSON to string.

     import java.util.LinkedHashMap; import java.util.Map; import org.json.JSONObject; public class cdf { public static void main(String[] args) { Map<String,String > myLinkedHashMap = new LinkedHashMap<String, String>(); myLinkedHashMap.put("1","first"); myLinkedHashMap.put("2","second"); myLinkedHashMap.put("3","third"); JSONObject json = new JSONObject(myLinkedHashMap); System.out.println(json.toString()); } } 

Output:

 {"3":"third","2":"second","1":"first"} . 

But I want it in the order of key insertion, for example:

 {"1":"first","2":"second","3":"third"} 

As soon as I convert LinkedHashMap to JSON, it loses its order (it is obvious that JSON has no concept of order), and therefore the line is also out of order. Now, how do I create a JSON string with the same order as LinkedHashMap ?

+6
source share
4 answers

Gson if your friend. This will print the ordered map into an ordered JSON string.

I used the latest version of Gson (2.3.1), you can download it through the following options at the bottom of this publication.

 import java.util.LinkedHashMap; import java.util.Map; import com.google.gson.Gson; public class OrderedJson { public static void main(String[] args) { // Create a new ordered map. Map<String,String> myLinkedHashMap = new LinkedHashMap<String, String>(); // Add items, in-order, to the map. myLinkedHashMap.put("1", "first"); myLinkedHashMap.put("2", "second"); myLinkedHashMap.put("3", "third"); // Instantiate a new Gson instance. Gson gson = new Gson(); // Convert the ordered map into an ordered string. String json = gson.toJson(myLinkedHashMap, LinkedHashMap.class); // Print ordered string. System.out.println(json); // {"1":"first","2":"second","3":"third"} } } 

Dependency Options

Maven

 <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.3.1</version> </dependency> 

Gradle

 compile 'com.google.code.gson:gson:2.3.1' 

Or you can visit Maven Central for more download options.

+10
source

Create a JsonArray for an ordered pair ...

 JSONArray orderedJson=new JSONArray(); Iterator iterator= ; while (iterator.hasNext()) { Map.Entry me = (Map.Entry)iteratornext(); System.out.print(me.getKey() + ": "); System.out.println(me.getValue()); JSONObject tmpJson=new JSONObject (); tmpJson.put(me.getKey(),me.getValue());//add key/value to tmpjson orderedJson.add(tmpJson);//add tmpjson to orderedJson } 
0
source

JSON does not accept the insertion order due to being linked to linkedhashmap parameters, both are strings. Is it possible to change the first parameter as Integer, as the code below:

 Map<Integer,String > myLinkedHashMap = new LinkedHashMap<>(); myLinkedHashMap.put(1,"first"); myLinkedHashMap.put(2,"second"); myLinkedHashMap.put(3,"third"); System.out.println(myLinkedHashMap); JSONObject json = new JSONObject(myLinkedHashMap); System.out.println(json.toString()); 
0
source

To sort alphabetically, here is the correct way with Jackson 2. *:

 Map<String, String> myLinkedHashMap = new LinkedHashMap<String, String>(); myLinkedHashMap.put("1", "first"); myLinkedHashMap.put("2", "second"); myLinkedHashMap.put("3", "third"); ObjectMapper mapper = new ObjectMapper(); mapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true); try { System.out.println(mapper.writeValueAsString(myLinkedHashMap)); //prints {"1":"first","2":"second","3":"third"} } catch (JsonProcessingException e) { e.printStackTrace(); } 
0
source

Source: https://habr.com/ru/post/984783/


All Articles