GSON fromJson()返回空attrubutes对象 [英] Gson fromJson() returns object with null attrubutes

查看:169
本文介绍了GSON fromJson()返回空attrubutes对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个使用此行的一些Java对象:

I'm trying to create some Java objects using this line:

测验currentQuiz = gson.fromJson(JSON,Quiz.class);

但我得到的是这样的:

下面是我的对象类:

提问:

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;
} 
}

问:

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;
}
}

答:

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;
}
}

这是我的JSON:

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
                }
            ]
        }
    ]
}
}

任何想法,为什么出现这种情况?我没有得到一个错误信息或LogCat中输出。

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

推荐答案

从您的JSON:我看到的是,在根级是像

From your json: i see that, at the root level it is something like

{测验:{REF有quizObject等,}}

{quiz:{quizObject having ref,etc.,}}

所以,你需要获得下一层开始使用GSON解析。

So, you need to get one level down to start parsing using gson.

所以,尝试了这一点,

JSONObject quizObject = json.get("quiz");

Quiz currentQuiz = gson.fromJson(quizObject.toString(), Quiz.class);

这篇关于GSON fromJson()返回空attrubutes对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆