Gson Fromjson() Returns Object With Null Attrubutes
I'm trying to create some Java objects using this line: Quiz currentQuiz = gson.fromJson(json, Quiz.class); But the all I get is this: Here are my object classes: Quiz: public cla
Solution 1:
From your json: i see that, at the root level it is something like
{quiz:{quizObject having ref,etc.,}}
So, you need to get one level down to start parsing using gson.
So, try this out,
JSONObject quizObject = json.get("quiz");
Quiz currentQuiz = gson.fromJson(quizObject.toString(), Quiz.class);
Solution 2:
In my case I had deleted
@SerializedName("OpCode")
@Expose
parts from my model class which are above the
privateInteger opCode;
line. And so Gson could not parse it and so my attributes were null. When I added those lines it fixed.
Solution 3:
Try this...
JSONObject js = newJSONObject(jsonString);
for (int i = 0; i < js.length(); i++) {
JSONObject quiz = js.getJSONObject("quiz");
for (int j = 0; j < js.length(); j++) {
String broadcast_dt = quiz.getString("broadcast_dt");
String ref = quiz.getString("ref");
JSONArray questions = quiz.getJSONArray("questions");
for (int k = 0; k < questions.length(); k++) {
String value = questions.getString(k);
JSONObject quest = newJSONObject(questions.getString(k));
int question_number = quest.getInt("question_number");
String question_text = quest.getString("question_text");
JSONArray answers = quest.getJSONArray("answers");
for (int m = 0; m < answers.length(); m++) {
JSONObject ans = newJSONObject(answers.getString(m));
Boolean correct_yn = ans.getBoolean("correct_yn");
String answer_text = ans.getString("answer_text");
}
}
}
}
Post a Comment for "Gson Fromjson() Returns Object With Null Attrubutes"