从LinkedHashMap构建有序的JSON字符串 [英] Building ordered JSON String from LinkedHashMap

查看:140
本文介绍了从LinkedHashMap构建有序的JSON字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要按照插入的顺序设置键/值对,所以我选择使用 LinkedHashMap 来覆盖 HashMap 。但是我需要将 LinkedHashMap 转换为JSON字符串,其中 LinkedHashMap 中的顺序保留在字符串中。 / p>

但是现在我可以通过以下方式实现它:


  1. 首先将LinkedHashMap转换为JSON。
  2. 然后将JSON转换为一个字符串。
    $ b

      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());


    code $


    输出是:

      {3:third,2:second,1: 第一}。 

    但是我想按照键的顺序插入它,如下所示:

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

    一旦我将 LinkedHashMap 转换为JSON,就会失去它的顺序显然JSON不具有顺序的概念),因此字符串也是失序的。现在,我该如何生成一个JSON字符串,其顺序与 LinkedHashMap

    解决方案< Gson 如果您的朋友。这将打印有序的地图成一个有序的JSON字符串。



    我使用最新版本的Gson(2.3.1),您可以通过下列选项这篇文章的底部。

      import java.util.LinkedHashMap; 
    import java.util.Map;

    import com.google.gson.Gson;

    public class OrderedJson {
    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);

    //实例化一个新的Gson实例。
    Gson gson = new Gson();

    //将有序映射转换为有序的字符串。
    String json = gson.toJson(myLinkedHashMap,LinkedHashMap.class);

    //打印有序的字符串。
    System.out.println(json); // {1:first,2:second,3:third}
    }
    }
    pre>

    依赖选项



    Maven



     <依赖项> 
    < groupId> com.google.code.gson< / groupId>
    < artifactId> gson< / artifactId>
    < version> 2.3.1< / version>
    < /依赖关系>



    Gradle



      compile'c​​om.google.code.gson:gson:2.3.1'

    或者您可以访问 Maven Central 获取更多下载选项。


    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?

    解决方案

    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.

    这篇关于从LinkedHashMap构建有序的JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆