em thử parse String Json này:
{"testId": 11, "questions": "[{\"questionId\": 1, \"answerId\": 1}]"}
đoạn xử lý :
ObjectMapper objMapper=new ObjectMapper();
System.out.print(result);
SurveyResponseResult survey = objMapper.readValue(result, SurveyResponseResult.class);
System.out.print(survey +"anv");
String prettyStaff1 = objMapper.writerWithDefaultPrettyPrinter().writeValueAsString(survey);
System.out.print(prettyStaff1);
class SurveyResponseResult :
public class SurveyResponseResult {
private int testId;
private SurveyQuestionAnswer questions;
public SurveyQuestionAnswer getQuestions() {
return questions;
}
public void setQuestions(SurveyQuestionAnswer questions) {
this.questions = questions;
}
public int getTestId() {
return testId;
}
public void setTestId(int testId) {
this.testId = testId;
}
public SurveyResponseResult(int testId, SurveyQuestionAnswer questions) {
super();
this.testId = testId;
this.questions = questions;
}
@Override
public String toString() {
return "SurveyResponseResult [questions=" + questions + ", testId="
+ testId + "]";
}
public SurveyResponseResult() {
super();
// TODO Auto-generated constructor stub
}
public class SurveyQuestionAnswer {
int questionId;
int answerId;
public int getQuestionId() {
return questionId;
}
public void setQuestionId(int questionId) {
this.questionId = questionId;
}
public int getAnswerId() {
return answerId;
}
public void setAnswerId(int answerId) {
this.answerId = answerId;
}
@Override
public String toString() {
return "SurveyQuestionAnswer {answerId=" + answerId + ", questionId="
+ questionId + "}";
}
public SurveyQuestionAnswer(int questionId, int answerId) {
super();
this.questionId = questionId;
this.answerId = answerId;
}
public SurveyQuestionAnswer() {
}
}
}
nhưng kết quả lại ra như thế này :
{"testId" : 11, "questions" : {"questionId" : 0,"answerId" : 0 }}
em đã làm sai từ chỗ nào vậy ạ.