zoukankan      html  css  js  c++  java
  • map --> json

    接下来介绍Map对象与json的相互转换

    Map类型的转换就需要用到

    GsonBuilder().enableComplexMapKeySerialization().create()类生成Gson对象
    1.      Map<String, String>类型的转换:

    这里直接贴关键代码,其他代码可参考前两篇文章,或下载代码。

    private String seven() {
    Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();
    Map<String, String> map = new HashMap<String, String>();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");

    return gson2.toJson(map);
    }
     

    结果为:


    把json数据转换为Map<String, String>:

    private String seven() {
    Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();
    Map<String, String> map = new HashMap<String, String>();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");

    String jsonString = gson.toJson(map);
    Type type = new TypeToken<Map<String, String>>() {}.getType();
    Map<String, String> map2 = gson2.fromJson(jsonString, type);

    String showString = "";
    for (String keyString : map2.keySet()) {
    showString += keyString + ":" + map2.get(keyString) + " ----- ";
    }

    showString += "---------------------- ";
    return showString;
    }
     

    结果为:


    2.      Map<Object, String>类型的转换:

    private String eight() {
    Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();
    Map<ToJsonBeanOne, String> map = new LinkedHashMap<ToJsonBeanOne, String>();
    map.put(new ToJsonBeanOne(1, "小熊1", 21), "value1");
    map.put(new ToJsonBeanOne(2, "小熊2", 21), "value2");
    map.put(new ToJsonBeanOne(3, "小熊3", 21), "value3");

    return gson2.toJson(map);
    }


    结果为:


    把json数据转换为Map<Object, String>:

    private String eight() {
    Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();
    Map<ToJsonBeanOne, String> map = new LinkedHashMap<ToJsonBeanOne, String>();
    map.put(new ToJsonBeanOne(1, "小熊1", 21), "value1");
    map.put(new ToJsonBeanOne(2, "小熊2", 21), "value2");
    map.put(new ToJsonBeanOne(3, "小熊3", 21), "value3");

    String jsonString = gson2.toJson(map);

    Type type = new TypeToken<Map<ToJsonBeanOne, String>>() {}.getType();
    Map<ToJsonBeanOne, String> map2 = gson.fromJson(jsonString, type);

    String showString = "";
    for (ToJsonBeanOne beanOne : map2.keySet()) {
    showString += "key: " + beanOne.toString() + "value:" + map2.get(beanOne) + " ----- ";
    }

    showString += "---------------------- ";
    return showString;
    }
    结果为:


     

    3. Map<String, Object>类型的转换:
    private String nine() {
    Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();
    Map<String, ToJsonBeanOne> map = new LinkedHashMap<String, ToJsonBeanOne>();
    map.put("key1", new ToJsonBeanOne(1, "小熊1", 21));
    map.put("key2", new ToJsonBeanOne(2, "小熊2", 21));
    map.put("key3", new ToJsonBeanOne(3, "小熊3", 21));

    return gson2.toJson(map);
    }
     

    结果为:


     

    把json数据转换为Map<String, Object>:

    private String nine() {
    Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();
    Map<String, ToJsonBeanOne> map = new LinkedHashMap<String, ToJsonBeanOne>();
    map.put("key1", new ToJsonBeanOne(1, "小熊1", 21));
    map.put("key2", new ToJsonBeanOne(2, "小熊2", 21));
    map.put("key3", new ToJsonBeanOne(3, "小熊3", 21));
    String jsonString = gson2.toJson(map);

    Type type = new TypeToken<Map<String, ToJsonBeanOne>>() {}.getType();
    Map<String, ToJsonBeanOne> map2 = gson.fromJson(jsonString, type);

    String showString = "";
    for (String keyString : map2.keySet()) {
    showString += "key: " + keyString + " value: " + map2.get(keyString).toString() + "----- ";
    }

    showString += "---------------------- ";
    return showString;
    }
     

    结果为:

    原文链接:https://blog.csdn.net/a249900679/article/details/51386660

  • 相关阅读:
    使用AOP 实现Redis缓存注解,支持SPEL
    springmvc 双亲上下文导致的 No mapping found for HTTP request
    调用iframe 中的js[兼容各种浏览器]
    NIO学习:异步IO实例
    byte数组与对象之间的相互转换
    NIO学习:buffer读入与写出(文件复制示例)
    NIO学习:使用Channel、Buffer写入文件
    使用摘要流获取文件的MD5
    ActiveMQ入门实例
    获取java项目 classpath目录
  • 原文地址:https://www.cnblogs.com/redBack/p/12262473.html
Copyright © 2011-2022 走看看