com.fasterxml.jackson.databind.JsonMappingException:无法从START_ARRAY标记

时间:2017-04-14 16:14:04

标签: java json parsing jackson objectmapper

给出以下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:不能   反序列化file.readers.TestAcceptanceCriteria的实例   [Source:results \ acceptanceCriteria.json;来自:来源:results \ acceptanceCriteria.json的START_ARRAY标记;线:   1,栏目:1] at   com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270)     在   com.fasterxml.jackson.databind.DeserializationContext.reportMappingException(DeserializationContext.java:1234)     在   com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1122)     在   com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1075)     在   com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromArray(BeanDeserializerBase.java:1374)     在   com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:174)     在   com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:150)     在   com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3798)     在   com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2740)     在   file.readers.AcceptanceCriteriaJsonReader.main(AcceptanceCriteriaJsonReader.java:51)

我的课程如下:

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

1 个答案:

答案 0 :(得分:0)

您正在尝试将实体数组反序列化为单个对象,这是导致问题的原因。我们试试吧:

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