Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Gson fromJson()返回属性为null的对象_Android_Json_Deserialization_Gson - Fatal编程技术网

Android Gson fromJson()返回属性为null的对象

Android Gson fromJson()返回属性为null的对象,android,json,deserialization,gson,Android,Json,Deserialization,Gson,我尝试使用以下行创建一些Java对象: quick-currentquick=gson.fromJson(json,quick.class) 但我得到的只是: 以下是我的对象类: 测验: 问题: public class Question { private int question_number; private String question_text; private Answer[] answers; public Question(){ } public Question(i

我尝试使用以下行创建一些Java对象:

quick-currentquick=gson.fromJson(json,quick.class)

但我得到的只是:

以下是我的对象类:

测验:

问题:

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:我看到,在根级别,它类似于

{测验:{quizObject有ref等,}

因此,您需要降低一个级别才能开始使用gson进行解析

那么,试试这个

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

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

JSONObject js = new JSONObject(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 = new JSONObject(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 = new JSONObject(answers.getString(m));
            Boolean correct_yn = ans.getBoolean("correct_yn");
            String answer_text = ans.getString("answer_text");
        }
    }

}

}
JSONObject js=新的JSONObject(jsonString);
对于(int i=0;i
就我而言,我已经删除了

@SerializedName("OpCode")
@Expose
我的模型类中高于

private Integer opCode;

线路。所以Gson无法解析它,所以我的属性为null。当我添加这些行时,它被修复了。

您是否检查了该行之前json的值?。它本身可能是空的!我有,它不是空的。它与上面粘贴的内容相同。谢谢,我正在使用GSON库。
private Integer opCode;