不废话,直接上代码:
public class GsonUtils { public static Gson getMapGson(){ Gson gson=new GsonBuilder().registerTypeAdapter(Map.class, new JsonDeserializer<Map>() { public Map 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; } }
使用方式
Gson gson = GsonUtils.getMapGson();