zoukankan      html  css  js  c++  java
  • Gson转Map时,Int会变成double解决方法

    package com.cdy.demo;
    
    import java.lang.reflect.Type;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Set;
    
    import com.google.gson.Gson;
    import com.google.gson.GsonBuilder;
    import com.google.gson.JsonDeserializationContext;
    import com.google.gson.JsonDeserializer;
    import com.google.gson.JsonElement;
    import com.google.gson.JsonObject;
    import com.google.gson.JsonParseException;
    
    public class GsonDoubleInteger {
    
        public static Gson getGson() {
            Gson gson = new GsonBuilder().registerTypeAdapter(HashMap.class, new JsonDeserializer<HashMap>() {
                @Override
                public HashMap<String, Object> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
                    HashMap<String, Object> resultMap = new HashMap<>();
                    JsonObject jsonObject = json.getAsJsonObject();
                    Set<Map.Entry<String, JsonElement>> entrySet = jsonObject.entrySet();
                    for (Map.Entry<String, JsonElement> entry : entrySet) {
                        resultMap.put(entry.getKey(), entry.getValue());
                    }
                    return resultMap;
                }
                
            }).create();
            return gson;
        }
        
        @SuppressWarnings("unchecked")
        public static void main(String[] args) {
            String aaa = "{ a: 1, b: 2.3 }";
            HashMap<String, Object> map = getGson().fromJson(aaa, HashMap.class);
            System.out.println(map);
        }
    }
  • 相关阅读:
    shell 字符串替换
    shell 拆分字符串成数组 放入数组
    shell 换行输出变量 换行
    Linux shell修改xml文件
    Spark 实现共同好友
    Hive 开启 service2 服务
    hive 求相互是好友.
    Linux 查看外网ip
    Termux下开启kex远程桌面
    Termux开启ssh服务
  • 原文地址:https://www.cnblogs.com/huangwentian/p/10526923.html
Copyright © 2011-2022 走看看