我已使用在线转换器http://pojo.sodhanalibrary.com/将answer.json
转换为java类。我在json中有硬编码的值,但是当我尝试使用AnswerTest.java访问这些值时,我正在关注" OUTPUT":
ClassPojo [Section_Name = null, Answers = null, User_Id = null, Form_Name = null, Answers1 = null]
为什么我得到空值?
{
"User_Id": "abcd",
"Form_Name": "Form",
"Answers": [{
"Question_Id": "1",
"Answer" :"a"
},
{
"Question_Id": "2",
"Answer" :"b"
},
{
"Question_Id": "3",
"Answer" :"c"
}
],
"Section_Name": "Project Overview",
"Answers1": [{
"Question_Id": "4",
"Answer" :"d"
},
{
"Question_Id": "5",
"Answer" :"e"
},
{
"Question_Id": "6",
"Answer" :"f"
}
]
}
这些是为上面的json生成的类:
public class Answer
{
private String Section_Name;
private Answers[] Answers;
private String User_Id;
private String Form_Name;
private Answers1[] Answers1;
public String getSection_Name ()
{
return Section_Name;
}
public void setSection_Name (String Section_Name)
{
this.Section_Name = Section_Name;
}
public Answers[] getAnswers ()
{
return Answers;
}
public void setAnswers (Answers[] Answers)
{
this.Answers = Answers;
}
public String getUser_Id ()
{
return User_Id;
}
public void setUser_Id (String User_Id)
{
this.User_Id = User_Id;
}
public String getForm_Name ()
{
return Form_Name;
}
public void setForm_Name (String Form_Name)
{
this.Form_Name = Form_Name;
}
public Answers1[] getAnswers1 ()
{
return Answers1;
}
public void setAnswers1 (Answers1[] Answers1)
{
this.Answers1 = Answers1;
}
@Override
public String toString()
{
return "ClassPojo [Section_Name = "+Section_Name+", Answers = "+Answers+", User_Id = "+User_Id+", Form_Name = "+Form_Name+", Answers1 = "+Answers1+"]";
}
}
public class Answers
{
private String Question_Id;
private String Answer;
public String getQuestion_Id ()
{
return Question_Id;
}
public void setQuestion_Id (String Question_Id)
{
this.Question_Id = Question_Id;
}
public String getAnswer ()
{
return Answer;
}
public void setAnswer (String Answer)
{
this.Answer = Answer;
}
@Override
public String toString()
{
return "ClassPojo [Question_Id = "+Question_Id+", Answer = "+Answer+"]";
}
}
public class Answers1
{
private String Question_Id;
private String Answer;
public String getQuestion_Id ()
{
return Question_Id;
}
public void setQuestion_Id (String Question_Id)
{
this.Question_Id = Question_Id;
}
public String getAnswer ()
{
return Answer;
}
public void setAnswer (String Answer)
{
this.Answer = Answer;
}
@Override
public String toString()
{
return "ClassPojo [Question_Id = "+Question_Id+", Answer = "+Answer+"]";
}
}
这是我从json打印数据的测试类。
public class AnswerTest {
public static void main(String[] args) throws JsonParseException,
JsonMappingException, IOException {
try {
File jsonFile = new File("answer.json");
Answer answer = null;
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(
DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
answer = objectMapper.readValue(jsonFile, Answer.class);
System.out.println(answer);
}
catch (Exception e) {
e.printStackTrace();
}
}
}