测试我的JSON文件并获得一个;“未识别的属性异常”;java中的错误
我在测试类中读取JSON文件以查看其是否有效时出现此错误,我不明白为什么。它经过验证,因此JSON文件没有问题。 任何帮助都将不胜感激 我的测试班:测试我的JSON文件并获得一个;“未识别的属性异常”;java中的错误,java,json,jackson,Java,Json,Jackson,我在测试类中读取JSON文件以查看其是否有效时出现此错误,我不明白为什么。它经过验证,因此JSON文件没有问题。 任何帮助都将不胜感激 我的测试班: public class ObjectToJsonFile { public static void main(String[] args) { ObjectMapper mapper = new ObjectMapper(); File file = new File("question.json"); try
public class ObjectToJsonFile {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
File file = new File("question.json");
try {
// Deserialize JSON file into Java object.
Question question= mapper.readValue(file, Question.class);
System.out.println("category.getCategory() = " + question.getCategory());
} catch (IOException e) {
e.printStackTrace();
}
}
我的答案是这样的:
public class Answer
{
@JsonProperty("answer")
private String answer;
@JsonProperty("correct")
private Boolean correct;
//setters and getters
}
我的问题课是这样的:
公开课问题{
@JsonProperty("category")
private String category;
@JsonProperty("clue")
private String clue;
@JsonProperty("Answers")
private List<Answer> answers;
@JsonProperty("questionTitle")
private String questionTitle;
//setters an getters
}
@JsonProperty(“类别”)
私有字符串类别;
@JsonProperty(“线索”)
私密线索;
@JsonProperty(“答案”)
私人名单答案;
@JsonProperty(“问题标题”)
私有字符串标题;
//二传手和传手
}
课堂上我的问题:
public class Questions {
@JsonProperty("Questions")
private List<Questions> questions;
@JsonCreator
public Questions(List<Questions> questions) {
super();
this.questions = questions;
}
// setters and getters
}
公开课问题{
@JsonProperty(“问题”)
私人名单问题;
@JsonCreator
公开问题(列表问题){
超级();
这个。问题=问题;
}
//二传手和接球手
}
您有很多小错误。缺少注释、字段名称大小写错误或将问题
与问题
混淆,请键入main()
。我或多或少修复了下面的代码,请参见下面代码中的注释:
答复:
public class Answer {
@JsonProperty("answer")
private String answer;
@JsonProperty("Correct") // was "correct"
private Boolean correct;
问题:
public class Questions {
@JsonProperty("Questions")
private List<Question> questions;
@JsonCreator
public Questions(@JsonProperty("Questions") List<Question> questions) { // was missing @JsonProperty
this.questions = questions;
}
在编写用于json数组映射的java类时,请非常小心,并验证将通过setter方法注入的每个字段。您的问题在下面的代码中。只是你犯的一个小错误
@JsonProperty("Questions")
private List<Question> questions;//it should be Question object
@JsonCreator
public Questions(List<Question> questions)//change the constructor accordingly
@JsonProperty(“问题”)
私人名单问题//它应该是问题对象
@JsonCreator
公共问题(列出问题)//相应地更改构造函数
如果仍然存在问题,只需检查所有可能的注射是否正确
祝你好运 现在我还纠正了其他错误。如果我是你,我会避免
@JsonProperty
和其他强制重写,而是将JSON更改为使用camelCase字段。你介意接受答案并进行投票吗?:)您可能希望在JSON中的Queston
数组中添加另一个Queston
对象,该对象具有不同的category
值,然后在main()中提取它
但您的问题陈述有点含糊不清。如果您使用的是Java 8,则可以使用Lambda筛选出您的问题questions root=mapper.readValue(文件,questions.class);List mathQuestions=root.getQuestions().stream().filter(q->q.getCategory().equals(“数学”).collect(Collectors.toList())代码>不确定“我的答案”是什么意思。每个问题都有多个答案
,所以我不知道哪个是“我的”。
@JsonProperty("Questions")
private List<Question> questions;//it should be Question object
@JsonCreator
public Questions(List<Question> questions)//change the constructor accordingly