Building ordered JSON String from LinkedHashMap

2019-02-05 03:54发布

I had a need for having Key/Value pairs in the order of my insertion, so I opted to use LinkedHashMap over HashMap. But I need to convert the LinkedHashMap into a JSON String where the order in the LinkedHashMap is maintained in the string.

But currently I'm achieving it by:

  1. First converting the LinkedHashMap into JSON.
  2. Then converting the JSON into a 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());
        }
    }
    

The output is:

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

But I want it in the order of insertion of the keys, like this:

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

Once I convert the LinkedHashMap into a JSON it loses it order (it's obvious that JSON doesn't have the notion of order) and hence the string too is out of order. Now, how do I generate a JSON string whose order is same as the LinkedHashMap?

6条回答
Bombasti
2楼-- · 2019-02-05 04:09

To sort alphabetically, here is the proper 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();
}
查看更多
手持菜刀,她持情操
3楼-- · 2019-02-05 04:19

JSONObject implies use of org.json library. Don't do that; it is old prototype and there are better options like Jackson and GSON. With Jackson you would just use:

String json = new ObjectMapper().writeValueAsString(myLinkedHashMap);

and get JSON string with entries in whatever traversal order your Map uses.

查看更多
我命由我不由天
4楼-- · 2019-02-05 04:20

JSON not taking insertion order due to linkedhashmap params both are string. Is it fine to change first param as Integer like below mentioned code:

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());
查看更多
劫难
5楼-- · 2019-02-05 04:20

I ran into the same problem. I had to use this particular library, and the first element must be with the key "$type". Here is my decision:

import org.json.JSONObject;

import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;


class TypedJSONObject extends JSONObject {

    private static final String KEY = "$type";

    @Override
    public Set<Map.Entry<String, Object>> entrySet() {
        Set<Map.Entry<String, Object>> superSet = super.entrySet();
        Set<Map.Entry<String, Object>> returnSet = new LinkedHashSet<>();

        HashMap<String, Object> map = new HashMap<>();
        for (Map.Entry<String, Object> entry : superSet) {
            if (entry.getKey().equals(KEY)) {
                map.put(KEY, entry.getValue());
                break;
            }
        }
        Set<Map.Entry<String, Object>> entries = map.entrySet();

        returnSet.addAll(entries);
        returnSet.addAll(superSet);

        return returnSet;
    }
}

As you see I overrite method entrySet, that used in method toString

查看更多
我命由我不由天
6楼-- · 2019-02-05 04:22

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 can download it via the following options at the bottom of this post.

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.

查看更多
小情绪 Triste *
7楼-- · 2019-02-05 04:29

Create JsonArray for 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
    }
查看更多
登录 后发表回答