Java GSON将转换为LinkedHashMap而不是我的对象

Java GSON将转换为LinkedHashMap而不是我的对象,java,gson,Java,Gson,我有一段代码: public abstract class Repository<Entity extends BaseObject> { ... public void readFromJson(){ String content = "JSON content here"; Gson gson = new Gson(); Type entityType = new TypeToken<JSONObject<Entity>>(){

我有一段代码:

public abstract class Repository<Entity extends BaseObject> {

...

public void readFromJson(){
    String content = "JSON content here";
    Gson gson = new Gson();
    Type entityType = new TypeToken<JSONObject<Entity>>(){}.getType();

    jsonObject = gson.fromJson(content, entityType);

    for (Entity ent : jsonObject.getEntities()) ;
    }
}
终于找到了

问题是:

新的TypeToken(){}.getType()

返回JSONObject的类型,而不是扩展存储库的子类的特定实体(例如UserRepository extends Repository

诀窍是创建一个抽象方法,强制子类设置反序列化的类型


总之,如果您遇到此错误,请确保您具有正确的类类型(如果您使用子类,请确保它返回的是您的子类而不是超类的类型)。

我也遇到了同样的问题。要在稍微不同的背景下给出更清晰的答案:

我有以下方法产生错误“com.google.gson.internal.LinkedTreeMap无法转换为MyType”:


对不起,我遇到了同样的问题,但我不明白你的答案,你能澄清一下吗?谢谢@JonathanLeung:如果我理解正确,问题是表达式
newTypeToken(){}
的每次执行都是实例化同一匿名类的实例<代码>实体实际上不是一个类,它只是
存储库
的一个类型参数,因此在这一点上它被完全删除,甚至不适合反射。(TypeTokenstuff使用反射来检查类层次结构以绕过擦除,但当类本身引用类型参数时,它不起作用。)抱歉。你的答案也不清楚。如果你能提供一个有意义的解决方案那就太好了。@ajith他确实提供了一个有意义的解决方案。他的意思是说,在具体的类中实现一个返回类类型(例如return User.class)的方法,因为T只是一个类型参数,而不是类类型,因为这是gson所寻找的。主要是
gsonBuilder.enableComplexMapKeySerialization()
解决了我的问题
public class JSONObject<Entity> {

private List<Entity> entities = new ArrayList<Entity>();
private long lastId = -1;
public List<Entity> getEntities() {
    return entities;
}
public void setEntities(List<Entity> entities) {
    this.entities = entities;
}
public long getLastId() {
    return lastId;
}
public void setLastId(long lastId) {
    this.lastId = lastId;
}

public void incrementLastId() {
    this.lastId++;
}
public abstract class BaseObject implements Serializable {

protected long id = (long) -1;
protected int version = 0;

protected BaseObject(){}

public long getId() {
    return id;
}
public void setId(long id) {
    this.id = id;
}
public int getVersion() {
    return version;
}
public void setVersion(int version) {
    this.version = version;
}

}
/**
   * Reads a LinkedHashMap from the specified parcel.
   * 
   * @param <TKey>
   *          The type of the key.
   * @param <TValue>
   *          The type of the value.
   * @param in
   *          The in parcel.
   * @return Returns an instance of linked hash map or null.
   */
  public static <TKey, TValue> LinkedHashMap<TKey, TValue> readLinkedHashMap(Parcel in) {
    Gson gson = JsonHelper.getGsonInstance();
    String content = in.readString();
    LinkedHashMap<TKey, TValue> result = gson.fromJson(content, new TypeToken<LinkedHashMap<TKey, TValue>>(){}.getType());
    return result;
  }
/**
   * Reads a LinkedHashMap from the specified parcel.
   * 
   * @param <TKey>
   *          The type of the key.
   * @param <TValue>
   *          The type of the value.
   * @param in
   *          The in parcel.
   * @return Returns an instance of linked hash map or null.
   */
  public static <TKey, TValue> LinkedHashMap<TKey, TValue> readLinkedHashMap(Parcel in, TypeToken<LinkedHashMap<TKey, TValue>> typeToken) {
    Gson gson = JsonHelper.getGsonInstance();
    Type type = typeToken.getType();
    String content = in.readString();
    LinkedHashMap<TKey, TValue> result = gson.fromJson(content, type);
    return result;
  }
readLinkedHashMap(in, new TypeToken<LinkedHashMap<UUID, MyObject>>(){});
gsonBuilder.enableComplexMapKeySerialization()