给定以下JSON文件:
[
{"name":"test_12345_54321",
"acceptanceCriteria":
[
{
"given":"I am a developer",
"and":"I have some test case to write",
"when":"I run my test",
"then":"I get a report generated about its details."
}
]
},
{"name":"test_2",
"acceptanceCriteria":
[
{
"given":"I am a developer test 2",
"and":"I have some test case to write test 2",
"when":"I run my test test 2",
"then":"I get a report generated about its details.2"
}
]
}
]
我试图使用Jackson对象映射器访问JSON文件中的值。具体如下:
File jsonFile = new File("results/acceptanceCriteria.json");
ObjectMapper mapper = new ObjectMapper();
TestAcceptanceCriteria acceptanceCriteria = null;
acceptanceCriteria = mapper.readValue(jsonFile, TestAcceptanceCriteria.class);
System.out.println(acceptanceCriteria.getAcceptanceCriteria());
public class AcceptanceCriteria{
private String given;
private String and;
private String when;
private String then;
public String getGiven(){
return given;
}
public void setGiven(String given){
this.given = given;
}
public String getAnd(){
return and;
}
public void setAnd(String and){
this.and = and;
}
public String getWhen(){
return when;
}
public void setWhen(String when){
this.when = when;
}
public String getThen(){
return then;
}
public void setThen(String then){
this.then = then;
}
}
import java.util.List;
public class TestAcceptanceCriteria {
private String name;
private List<AcceptanceCriteria> acceptanceCriteria;
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public List<AcceptanceCriteria> getAcceptanceCriteria(){
return acceptanceCriteria;
}
public void setAcceptanceCriteria(List<AcceptanceCriteria> acceptanceCriteria){
this.acceptanceCriteria = acceptanceCriteria;
}
}
您正在尝试将实体数组反序列化为单个对象,这导致了一个问题。让我们试试:
List<TestAcceptanceCriteria> acceptanceCriteria = null;
acceptanceCriteria = mapper.readValue(json, new TypeReference<List<TestAcceptanceCriteria>>(){});
下面提到的是JSON字符串ResultString: 我想将相同的内容转换为A类的对象: B类代码为: 但是获取异常: JSONMappingException:无法从START_ARRAY标记反序列化java.util.LinkedHashMap实例
但是我得到了这个错误: “无法读取文档:无法反序列化com.advice.domain.family.income实例,从[source:java.io.pushbackInputStream@3d3298f9;行:1,列:2]处的START_ARRAY令牌中取出(通过引用链:java.util.ArrayList[0]);嵌套异常是com.fasterxml.jackson.databind.j
看起来杰克逊无法处理同名的属性和属性。
问题内容: 我正在尝试使用Jackson将json数据转换为POJO对象。这是MainActivity和我的POJO类代码。我基本上收到了JsonMappingException错误。我还附上了整个日志。 MainActivity.java: Entries.java(这是POJO) 现在,我的日志中出现以下错误。因此,我无法继续工作。这是日志: 问题答案: 在 条目* 和 电话中 删除构造函数