Gsonを使って配列型Json(JsonArray)をList型オブジェクトにパース
しようと思っているのですが、うまくいかず困っています。
どこらへんに問題があるのかアドバイスいただけると助かります
//json取得部分 StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { @Override public void onResponse(String response) { Gson gson = new Gson(); Type type = new TypeToken<List>(){}.getType(); List<ProblemSet> list = gson.fromJson(response,type); Log.v("Test",String.valueOf(list)); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e("test", "onErrorResponse: " ); } });
//json { "problems": [ { "question": { "id": 1, "content": "1 + 1 =", "correct_option_id": 2 }, "options": [ { "id": 1, "content": "1", "correct": false }, { "id": 2, "content": "2", "correct": true }, { "id": 3, "content": "3", "correct": false }, { "id": 4, "content": "4", "correct": false } ] }, { "problem": { "id": 2, "content": "2 + 3 =", "correct_option_id": 2 }, "options": [ { "id": 1, "content": "4", "correct": false }, { "id": 2, "content": "5", "correct": true }, { "id": 3, "content": "6", "correct": false }, { "id": 4, "content": "7", "correct": false } ] } ] }
//Gsonに整形 public class ProblemSet { private Question question; public Question getQuestion() { return question; } //1.questionのクラスを作成 private class Question{ private int id; private String content; private int correct_option_id; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public int getCorrect_option_id() { return correct_option_id; } public void setCorrect_option_id(int correct_option_id) { this.correct_option_id = correct_option_id; } } }
回答1件
あなたの回答
tips
プレビュー