2013-04-25 32 views
9

Tôi đang cố gắng để tạo ra một số đối tượng Java sử dụng dòng này:Gson fromJson() trả về đối tượng với attrubutes rỗng

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

Nhưng tất cả tôi nhận được là:

Quiz object after fromJson

Đây là các lớp đối tượng của tôi:

Câu hỏi:

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

Câu hỏi:

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

Trả lời:

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

Và đây là JSON của tôi:

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

Bất cứ ý tưởng tại sao điều này xảy ra? Tôi không nhận được thông báo lỗi hoặc đầu ra LogCat.

+0

bạn đã kiểm tra giá trị của json trước dòng đó chưa ?. chính nó có thể là vô giá trị! – SKK

+0

Tôi có, nó không phải là null. Nó giống với những gì được dán ở trên. Chúc mừng mặc dù :) –

Trả lời

16

Từ json của bạn: tôi thấy rằng, ở cấp độ gốc nó là một cái gì đó giống như

{đố:. {QuizObject có ref, vv,}}

Vì vậy, bạn cần phải nhận được một mức độ xuống để bắt đầu phân tích cú pháp bằng gson.

Vì vậy, hãy thử này ra,

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

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

Bạn là một con người xinh đẹp. –

+1

Mong đợi từJson mong đợi một chuỗi, do đó, nó là quizObject.toString() :) –

+0

cảm ơn vì đã chỉ ra điều đó. tôi sẽ sửa câu trả lời của tôi. Chúc mừng mã hóa. :) – SKK

-5

Hãy thử này ...

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

Cảm ơn, tôi đang sử dụng thư viện GSON. –

1

Trong trường hợp của tôi, tôi đã xóa

@SerializedName("OpCode") 
@Expose 

phần từ lớp mô hình của tôi mà là ở trên số

private Integer opCode; 

dòng. Và vì vậy Gson không thể phân tích nó và vì vậy các thuộc tính của tôi là vô giá trị. Khi tôi thêm những dòng đó nó cố định.

Các vấn đề liên quan