gpt4 book ai didi

android - Gson fromJson() 返回具有空属性的对象

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:02:11 25 4
gpt4 key购买 nike

我正在尝试使用这一行创建一些 Java 对象:

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

但我得到的是:

Quiz object after fromJson

这是我的对象类:

测验:

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:

{
"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 输出。

最佳答案

从你的 json 中:我看到,在根级别它类似于

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

所以,你需要降一级才能开始使用 gson 进行解析。

所以,试试这个,

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

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

关于android - Gson fromJson() 返回具有空属性的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16210787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com