2013-04-25 68 views
9

我想用這條線來創建一些Java對象:GSON fromJson()返回空attrubutes對象

Quiz 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輸出。

+0

你檢查過該行之前的json的值嗎?本身可能是空的! – SKK 2013-04-25 09:28:01

+0

我有,它不是空的。它與上面粘貼的內容相同。歡呼雖然:) – 2013-04-25 09:31:44

回答

16

從您的JSON:我看到的是,在根級別是像

{測驗:{有裁判等quizObject,}}

所以,你需要獲得下一層使用gson開始分析。

因此,嘗試了這一點,

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

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

你是一個美麗的人。 – 2013-04-25 09:53:00

+1

期待從Json期待一個字符串,所以它的quizObject.toString():) – 2013-04-25 09:53:22

+0

感謝指出。我會編輯我的答案。快樂的編碼。 :) – SKK 2013-04-25 09:59:10

-5

嘗試......

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

} 

} 
+0

謝謝,我使用GSON庫。 – 2013-04-25 10:30:12

1

在我來說,我已經從我的模型類,這是上面刪除

@SerializedName("OpCode") 
@Expose 

零件

private Integer opCode; 

一行。所以Gson無法解析它,所以我的屬性爲空。當我添加這些行時就修復了。