Collections 使用gson 2反序列化集合的奇怪之处

Collections 使用gson 2反序列化集合的奇怪之处,collections,gson,Collections,Gson,我有这些课程 public class Application { public String name; public String ico; public List<MenuStruct> menu =new ArrayList<MenuStruct>(); //Constructor public Application() { } } public class MenuStruct { public String

我有这些课程

public class Application {
    public String name;
    public String ico;
    public List<MenuStruct> menu =new ArrayList<MenuStruct>();
    //Constructor
    public Application() {  }
}
public class MenuStruct {
    public String id;
    public String type;    
    public String parent;  
    public String name;    
    public String secId;   
    //Constructor   
    public MenuStruct() {}
}
我得到一个java.lang.ClassCastException

但如果我尝试反序列化它的containig类ApplicationManager,它不会失败

s="{ \"apps\": [ {\"name\":\"reg_salida\" , \"ico\":\"document-open-2-32x32.ico\" }]}";
ApplicationManager apm=(new Gson()).fromJson(s,ApplicationManager.class);
for (Application ap:apm.apps){
    System.out.println(ap.name); // now no errors here! and shows reg_salida
}
这是gson 2.2.4的错误吗?或者我做了一些不正确的事情


Eduard.

您必须提供属性类的完整定义。您的示例应该如下所示:

manager.apps = gson.fromJson(json, new TypeToken<List<Application>>() {}.getType());
manager.apps=gson.fromJson(json,newTypeToken(){}.getType());

我很高兴能帮助您。你能检查一下我的回答吗?这是一个有帮助的投票。
manager.apps = gson.fromJson(json, new TypeToken<List<Application>>() {}.getType());