Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

Quiz object after fromJson

Here are my 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 is my 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 a third question with some additional question text appearing here.",
            "answers": [
                {
                    "answer_text": "Cow",
                    "correct_yn": false
                },
                {
                    "answer_text": "Pig",
                    "correct_yn": true
                }
            ]
        }
    ]
}
}

Any ideas why this happens? I don't get an error message or LogCat output.

like image 302
MSpeed Avatar asked Apr 25 '13 09:04

MSpeed


People also ask

How do you handle null in Gson?

By default, the Gson object does not serialize the fields with null values to JSON. If a field in a Java object is null, Gson excludes it. We can force Gson to serialize null values via the GsonBuilder class. We need to call the serializeNulls() method on the GsonBuilder instance before creating the Gson object.

What is the use of Gson fromJson?

A Gson is a library for java and it can be used to generate a JSON. We can use the fromJson() method of Gson to parse JSON string into java object and use the toJson() method of Gson to convert Java objects into JSON string.

Does Gson ignore extra fields?

3. Deserialize JSON With Extra Unknown Fields to Object. As you can see, Gson will ignore the unknown fields and simply match the fields that it's able to.

How does Gson from JSON work?

Gson can work with arbitrary Java objects including objects for which you do not have the source. For this purpose, Gson provides several built in serializers and deserializers. A serializer allows to convert a Json string to corresponding Java type. A deserializers allows to convert from Java to a JSON representation.


1 Answers

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);
like image 80
SKK Avatar answered Oct 26 '22 22:10

SKK