• 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

  • 相关阅读:
    java基础面试题(一)
    关于Redis的十个高频面试问题
    postgres中的merge join
    cgdb调试postgresql
    再见了,程序员!
    掌握这个小技巧,让你的 C++ 编译速度提升 50 倍!
    C语言修仙大法!基础知识全复习(纯干货)!!!
    一锅端了!北京朝阳一互联网公司被端,警方上门,23人被带走…
    C++入口不是main?知乎上都快打起来了,你们不要再打了啦!
    不用创建项目,直接在 VS 里快速测试 C/C++ 代码 !
  • 原文地址:https://www.cnblogs.com/redBack/p/12262473.html
Copyright © 2020-2023  润新知