gpt4 book ai didi

java - com.fasterxml.jackson.databind.JsonMappingException : Can not deserialize instance of TestAcceptanceCriteria out of START_ARRAY token

转载 作者:行者123 更新时间:2023-12-04 17:52:34 27 4
gpt4 key购买 nike

给定以下 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 Object Mapper 访问 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());

每次我尝试访问任何变量时,我都会收到以下错误:

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of file.readers.TestAcceptanceCriteria out of START_ARRAY token at [Source: results\acceptanceCriteria.json; line: 1, column: 1] at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270) at com.fasterxml.jackson.databind.DeserializationContext.reportMappingException(DeserializationContext.java:1234) at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1122) at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1075) at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromArray(BeanDeserializerBase.java:1374) at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:174) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:150) at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3798) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2740) at file.readers.AcceptanceCriteriaJsonReader.main(AcceptanceCriteriaJsonReader.java:51)

我的类(class)如下:

 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>>(){});

关于java - com.fasterxml.jackson.databind.JsonMappingException : Can not deserialize instance of TestAcceptanceCriteria out of START_ARRAY token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43415285/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com