android - Gson fromJson() returns object with null attrubutes -
i'm trying create java objects using line:
quiz currentquiz = gson.fromjson(json, quiz.class);
but this:
here object classes:
quiz:
public class quiz { private string ref; private string broadcast_dt; private question[] questions; public quiz() { // todo auto-generated constructor stub } public string getref() { return ref; } public void setref(string ref) { this.ref = ref; } public string getbroadcast_dt() { return broadcast_dt; } public void setbroadcast_dt(string broadcast_dt) { this.broadcast_dt = broadcast_dt; } public quiz(string ref, string broadcast_dt, question[] questions) { super(); this.ref = ref; this.broadcast_dt = broadcast_dt; this.questions = questions; } public question[] getquestions() { return questions; } public void setquestions(question[] questions) { this.questions = questions; } }
question:
public class question { private int question_number; private string question_text; private answer[] answers; public question(){ } public question(int question_number, string question_text, answer[] answers) { super(); this.question_number = question_number; this.question_text = question_text; this.answers = answers; } public int getquestion_number() { return question_number; } public void setquestion_number(int question_number) { this.question_number = question_number; } public string getquestion_text() { return question_text; } public void setquestion_text(string question_text) { this.question_text = question_text; } public answer[] getanswers() { return answers; } public void setanswers(answer[] answers) { this.answers = answers; } }
answer:
public class answer { private string answer_text; private boolean correct_yn; public answer(){ } public string getanswer_text() { return answer_text; } public void setanswer_text(string answer_text) { this.answer_text = answer_text; } public boolean iscorrect_yn() { return correct_yn; } public void setcorrect_yn(boolean corrent_yn) { this.correct_yn = corrent_yn; } }
and here json:
{ "quiz": { "ref": "45g36745bu46", "broadcast_dt": "2013-03-03t00:00:00z", "questions": [ { "question_number": 1, "question_text": "example question one", "answers": [ { "answer_text": "answer one", "correct_yn": false }, { "answer_text": "answer two", "correct_yn": true }, { "answer_text": "answer three", "correct_yn": false } ] }, { "question_number": 2, "question_text": "question number two", "answers": [ { "answer_text": "something", "correct_yn": false }, { "answer_text": "something else", "correct_yn": false }, { "answer_text": "another thing", "correct_yn": true } ] }, { "question_number": 3, "question_text": "and third question additional question text appearing here.", "answers": [ { "answer_text": "cow", "correct_yn": false }, { "answer_text": "pig", "correct_yn": true } ] } ] } }
any ideas why happens? don't error message or logcat output.
from json: see that, @ root level
{quiz:{quizobject having ref,etc.,}}
so, need 1 level down start parsing using gson.
so, try out,
jsonobject quizobject = json.get("quiz"); quiz currentquiz = gson.fromjson(quizobject.tostring(), quiz.class);