gson正确地反序列化一个对象但不是它的内部

时间:2016-06-09 09:16:29

标签: java json gson

我有这堂课:

public class Campaign 
{

String Campaign_ID;
String Campaign_Description;
String startQuestion;
ArrayList Question_Array;
ArrayList workflow;

public Campaign(String ID, String description, String start, ArrayList quesArray, ArrayList workflow)
{
    this.Campaign_ID = ID;
    this.Campaign_Description = description;
    this.startQuestion = start;
    this.Question_Array = quesArray;
    this.workflow = workflow;
}

public String getID() {return this.Campaign_ID;}
public String getDescription() {return this.Campaign_Description;}
public String getStartQuestion() {return this.startQuestion;}
public ArrayList getQuestionArray() {return this.Question_Array;}
public ArrayList getWorkflow() {return this.workflow;}

public Base_Question getQuestionByID(String ID){
    Base_Question result = null;
    for (int i=0;i<Question_Array.size();i++)
    {
        if (    ((Base_Question) Question_Array.get(i)).getQuestionID().equals(ID) ) 
            result = (Base_Question) Question_Array.get(i);
    }       
    return result;      
}

}

ArrayList Question_Array包含来自其他类的实例列表。 然后我初始化Campaign的实例:

Campaign campaign = new Campaign("Cam999","description blabla","Q001",list, workflow);

然后我将此实例序列化为文本:

Gson gson = new Gson();     
String text = gson.toJson(campaign);

要返回Campaign类的实例,我使用:

Campaign campaign2 = gson.fromJson(text, Campaign.class);

我可以正确地打印来自campaign2的内容,例如campaign2.getID()返回Cam999。但问题是campaign2中的ArrayList。 campaign2.getQuestionArray().get(index).getClass()打印 com.google.gson.internal.LinkedTreeMap ,而不是我的自定义类。

我的完整源代码在https://github.com/khoiboo/Class_Hierarchy

如何解决这个问题。我非常感谢你的帮助

0 个答案:

没有答案