简体   繁体   English

com.fasterxml.jackson.databind.JsonMappingException:无法从START_ARRAY标记中反序列化TestAcceptanceCriteria的实例

[英]com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of TestAcceptanceCriteria out of START_ARRAY token

Given the following JSON file: 给出以下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"
                     }
                   ]
        }
    ]

I am trying to access values within a JSON file using Jackson Object Mapper. 我试图使用Jackson Object Mapper访问JSON文件中的值。 As follows: 如下:

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

Each time I try to access any variable I get the following error: 每次我尝试访问任何变量时,都会收到以下错误:

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of file.readers.TestAcceptanceCriteria out of START_ARRAY token at [Source: results\\acceptanceCriteria.json; com.fasterxml.jackson.databind.JsonMappingException:无法在[Source:results \\ acceptanceCriteria.json;中的START_ARRAY标记中反序列化file.readers.TestAcceptanceCriteria的实例。 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(AcceptanceCriteria com:fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270)中com.fasterxml的com.fasterxml.jackson.databind.DeserializationContext.reportMappingException(DeserializationContext.java:1234)中的行:1,column:1] .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)com的com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:174)com的com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:150)。 quickxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3798)at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2740)at file.readers.AcceptanceCriteriaJsonReader.main(AcceptanceCriteria JsonReader.java:51) JsonReader.java:51)

My classes are as follows: 我的课程如下:

 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;
    }
}

You are trying deserialize array of entities to single object, what cause a problem. 您正在尝试将实体数组反序列化为单个对象,这是导致问题的原因。 Let's try: 我们试试吧:

List<TestAcceptanceCriteria> acceptanceCriteria = null;
acceptanceCriteria = mapper.readValue(json, new TypeReference<List<TestAcceptanceCriteria>>(){});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 com.fasterxml.jackson.databind.JsonMappingException:无法反序列化START_ARRAY令牌 - com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize out of START_ARRAY token 错误:com.fasterxml.jackson.databind.JsonMappingException:无法从START_ARRAY令牌中反序列化Entities.Student的实例 - Error: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of Entities.Student out of START_ARRAY token com.fasterxml.jackson.databind.JsonMappingException:无法从START_ARRAY令牌中反序列化org.springframework.data.domain.Sort的实例 - com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of org.springframework.data.domain.Sort out of START_ARRAY token com.fasterxml.jackson.databind.JsonMappingException:无法从START_OBJECT令牌中反序列化java.util.ArrayList实例 - com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从START_ARRAY令牌中反序列化对象的实例 - com.fasterxml.jackson.databind.exc.MismatchedInputException: Can not deserialize instance of object out of START_ARRAY token com.fasterxml.jackson.databind.JsonMappingException:无法反序列化VALUE_NUMBER_INT令牌中的java.util.ArrayList实例 - com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of VALUE_NUMBER_INT token com.fasterxml.jackson.databind.exc.MismatchedInputException:无法反序列化 Object 的实例超出 START_ARRAY 令牌 - com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of Object out of START_ARRAY token com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从 START_ARRAY 令牌中反序列化对象实例 - JAVA - com.fasterxml.jackson.databind.exc.MismatchedInputException: Can not deserialize instance of object out of START_ARRAY token - JAVA 引起:org.codehaus.jackson.map.JsonMappingException:无法从START_ARRAY令牌中反序列化com.model.user的实例 - Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of com.model.user out of START_ARRAY token com.fasterxml.jackson.databind.JsonMappingException 无法构造类的实例 - com.fasterxml.jackson.databind.JsonMappingException not able to construct instance of class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM