Java 使用Gson反序列化具有泛型对象类型的嵌套JSON

Java 使用Gson反序列化具有泛型对象类型的嵌套JSON,java,android,json,gson,Java,Android,Json,Gson,我目前正在为我的android应用程序使用一个基本的JSON库来提取从服务器发送的JSON。为了提高性能,我正在考虑转到Gson 目前,由于以下原因,我一直坚持反序列化- 我的课- public class GameResponse { public boolean failed = false; public Object jsonObject; // Type cast this object based on the class type passed in json s

我目前正在为我的android应用程序使用一个基本的JSON库来提取从服务器发送的JSON。为了提高性能,我正在考虑转到Gson

目前,由于以下原因,我一直坚持反序列化-

我的课-

public class GameResponse {

    public boolean failed = false;
    public Object jsonObject; // Type cast this object based on the class type passed in json string
}

public class GameBatchResponse {

    public GameResponse[] gameResponses;
}
反序列化我的jsonresponse-

 Gson gson = new Gson();

 GameBatchResponse response = gson.fromJson(jsonResponse, GameBatchResponse.class);
现在,如何告诉Gson它需要在哪个类中输入cast JsonObject。目前,它正在将其转换为LinkedTreeMap,因为它不知道需要将其类型转换为哪个类

当我执行MyClassresponse.gameResponses[0].jsonObject时,它会给出类强制转换异常

在当前的实现中,我曾经在Json字符串中传递@type,并将使用它来创建MyClass的实例。例如-@type:com.mypackage.MyClass

我正在寻找一个相同逻辑的Gson实现,我可以从运行时JSON字符串中附加的信息中告诉Gson类类型

public static Object createObjectFromJSON(String jsonString, Map<Class, AbstractAdapter>map,Class classType) {
        GsonBuilder builder = new GsonBuilder();
        if(map!=null) {
            for (Entry<Class, AbstractAdapter> entry : map.entrySet()) {
                builder.registerTypeAdapter(entry.getKey(), entry.getValue());
            }
        }
        builder.setPrettyPrinting();
        builder.serializeNulls();
        Gson  gsonExt = builder.create();
        return gsonExt.fromJson(jsonString, classType);
    }
您必须定义自己的AbstractAdapter类

public class Adapter extends AbstractAdapter{

        @Override
        public AbstractSureCallDataFile deserialize(JsonElement json, Type typeOfT,
                JsonDeserializationContext context) throws JsonParseException  {
            JsonObject jsonObject =  json.getAsJsonObject();
            JsonPrimitive prim = (JsonPrimitive) jsonObject.get(AbstractAdapter.CLASSNAME);
            String className = prim.getAsString();

            Class<?> klass = null;
            try {
                klass = Class.forName(className);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                throw new JsonParseException(e.getMessage());
            }
            return context.deserialize(jsonObject.get(AbstractAdapter.INSTANCE), klass);
        }

        @Override
        public JsonElement serialize(Serializable src, Type typeOfSrc,
                JsonSerializationContext context) {

            JsonObject retValue = new JsonObject();
            String className = src.getClass().getCanonicalName();
            retValue.addProperty(AbstractAdapter.CLASSNAME, className);
            JsonElement elem = context.serialize(src);
            retValue.add(AbstractAdapter.INSTANCE, elem);
            return retValue;
        }

}
电话将会是

Map<Class, AbstractAdapter> map=new HashMap<>();
                            map.put(Xyz.class, new Adapter());
                            Object obj= createObjectFromJSON(line, map, MainObjectClass.class);

为了帮助您,最好有一个您正试图反序列化的JSON示例