2015-02-24 176 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