2015-02-24 94 views
0

昨天我管理如何将json转换为列表<>,json文件非常简单。Gson - 转换表单json(List列表内)列表<> Android

现在我其中有一个列表中列出这里是一个儿子文件:

<

这里我的代码:

listq = new ArrayList<Survey>(); 

    try { 
     result = new RequestTask().execute("urveys/current?owner=ofc&user=yasser&category=-1").get(); 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (ExecutionException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
     GsonBuilder gsonb = new GsonBuilder(); 
     Gson gson = gsonb.create(); 
     listq = gson.fromJson(result, Surveyentity.class).List; 

     Toast.makeText(Questions.this, "result", Toast.LENGTH_SHORT).show(); 
} 

我的班级调查:

public class Survey { 

String IdQuestion; 
String Label; 
List<Reponse> Reponses; 
public String getIdQuestion() { 
    return IdQuestion; 
} 
public void setIdQuestion(String idQuestion) { 
    IdQuestion = idQuestion; 
} 
public String getLabel() { 
    return Label; 
} 
public void setLabel(String label) { 
    Label = label; 
} 
public List<Reponse> getReponses() { 
    return Reponses; 
} 
public void setReponses(List<Reponse> reponses) { 
    Reponses = reponses; 
} 

我的班级回复:

public class Reponse { 

String IdProposition; 
String Label; 
String Format; 
String url; 
String Points; 
String Next; 
String Comment; 
String Selected; 
public String getIdProposition() { 
    return IdProposition; 
} 
public void setIdProposition(String idProposition) { 
    IdProposition = idProposition; 
} 
............. 

和我的课调查单位:

public class Surveyentity { 

public List<Survey> List; 

}

如何处理此问题?

+0

'名单反应变量; '应该'列表列表;' – 2015-02-24 11:01:58

回答

0

更新您的调查类

public class Survey { 

String IdQuestion; 
String Label; 
List<Reponse> List; // it should be List not Responses 
public String getIdQuestion() { 
    return IdQuestion; 
} 
public void setIdQuestion(String idQuestion) { 
    IdQuestion = idQuestion; 
} 
public String getLabel() { 
    return Label; 
} 
public void setLabel(String label) { 
    Label = label; 
} 
public List<Reponse> getReponses() { 
    return Reponses; 
} 
public void setReponses(List<Reponse> reponses) { 
    Reponses = reponses; 
} 
+0

为什么它应该是列表;我认为这与名称无关 – 2015-02-24 12:22:15

+0

Gson使用反射来映射JSON中的字符串。反序列化时,Gson正在导航正在反序列化的对象的类型树。这会导致忽略JSON输入中存在的额外字段。 – 2015-02-24 12:26:41

+0

无论如何,我试图改变这一点,它不工作 – 2015-02-24 12:36:59