解决Gson解析json字符串,Integer变为Double类型的问题

直接上代码记录下。我代码里没有Gson包,用的是nacos对Gson的封装,只是包不同,方法都一样


import com.alibaba.nacos.shaded.com.google.common.reflect.TypeToken;
import com.alibaba.nacos.shaded.com.google.gson.*;

import java.util.Map;
import java.lang.reflect.*;
import java.util.*;

public class TestJson {
    static class MapDeserializer implements JsonDeserializer<Map<String, Object>> {
        @Override
        public Map<String, Object> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            if (json.isJsonObject()) {
                JsonObject jsonObject = json.getAsJsonObject();
                Map<String, Object> map = new HashMap<>();
                for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
                    String key = entry.getKey();
                    JsonElement value = entry.getValue();
                    if (value.isJsonPrimitive() && value.getAsJsonPrimitive().isNumber()) {
                        JsonPrimitive primitive = value.getAsJsonPrimitive();
                        if (primitive.isNumber()) {
                            if (primitive.getAsString().contains(".")) {
                                map.put(key, primitive.getAsDouble());
                            } else {
                                map.put(key, primitive.getAsInt());
                            }
                        }
                    } else if (value.isJsonObject()) {
                        map.put(key, deserialize(value, typeOfT, context));
                    } else if (value.isJsonArray()) {
                        map.put(key, processList(value.getAsJsonArray(), context));
                    } else if (value.isJsonNull()) {
                        map.put(key, null);
                    } else {
                        throw new JsonParseException("Unsupported value type: " + value.getClass());
                    }
                }
                return map;
            } else {
                throw new JsonParseException("Expected JsonObject, but got: " + json.getClass());
            }
        }

        private List<Object> processList(JsonArray array, JsonDeserializationContext context) {
            List<Object> list = new ArrayList<>();
            for (JsonElement element : array) {
                if (element.isJsonPrimitive() && element.getAsJsonPrimitive().isNumber()) {
                    JsonPrimitive primitive = element.getAsJsonPrimitive();
                    if (primitive.isNumber()) {
                        if (primitive.getAsString().contains(".")) {
                            list.add(primitive.getAsDouble());
                        } else {
                            list.add(primitive.getAsInt());
                        }
                    }
                } else if (element.isJsonObject()) {
                    list.add(deserialize(element, null, context));
                } else if (element.isJsonArray()) {
                    list.add(processList(element.getAsJsonArray(), context));
                } else if (element.isJsonNull()) {
                    list.add(null);
                } else {
                    throw new JsonParseException("Unsupported value type: " + element.getClass());
                }
            }
            return list;
        }
    }

    class BkR<T> {
        private boolean result;
        private T data;

        public boolean isResult() {
            return result;
        }

        public void setResult(boolean result) {
            this.result = result;
        }

        public T getData() {
            return data;
        }

        public void setData(T data) {
            this.data = data;
        }
    }

    public static void main(String[] args) {
        String json = "{\"result\": true, \"data\": {\"aaa\":1, \"bbb\":{\"ab\":[{\"a\":1},{\"a\":2}],\"ccc\":{\"bdd\":[111,222,333,444]}}}}";
        Gson gson = new GsonBuilder()
                .registerTypeAdapter(new TypeToken<Map<String, Object>>() {}.getType(), new MapDeserializer())
                .create();

        BkR<Map<String, Object>> bkr = gson.fromJson(json, new TypeToken<BkR<Map<String, Object>>>() {}.getType());
        System.out.println(bkr);
    }
}

输出信息:

解决Gson解析json字符串,Integer变为Double类型的问题_第1张图片

你可能感兴趣的:(json,开发语言,java)