无法从START_ARRAY标记中反序列化SaleListDTO的实例

时间:2017-08-04 16:55:46

标签: json jackson

问题是我收到此错误。

我必须模拟休息服务电话,因为它现在由另一个团队开发。

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.egencia.service.invoiceaggregator.cache.SaleListDTO out of START_ARRAY token

这是我的杰克逊映射器

Jackson2ObjectMapperBuilder
                .json()
                .featuresToEnable(DeserializationFeature.UNWRAP_ROOT_VALUE, DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)                                    .serializationInclusion(JsonInclude.Include.NON_NULL)
                .serializationInclusion(JsonInclude.Include.NON_EMPTY)
                .failOnUnknownProperties(false)
                .build();
@JsonRootName("list")
public class SaleListDTO {
    private SaleDTO[] list;
    public SaleDTO[] getList() {
        return list;
    }
    public void setList(SaleDTO[] list) {
        this.list = list;
    }
}

这是JsON文件

{"list": [
    {
        "id": 111111,
        "currency": "EUR",
        "country": "ITA",
        "name": "Italy",
        "code": "IT"
    },...
]}

我已经测试了这么多组合但是徒劳无功。请帮忙

1 个答案:

答案 0 :(得分:1)

删除@JsonRootName(" list")。

以下是工作示例:

@Getter
@Setter
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class SaleListDTO {

    @JsonProperty("list")
    private SaleDTO[] list;



}


@Getter
@Setter
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class SaleDTO {

    private int id;
    private String currency;
    private String country;
    private String name;
    private String code;   



}

测试方法:

@Test
    public void testConversion() throws JsonParseException, JsonMappingException, IOException{
        ObjectMapper mapper=new ObjectMapper();
        SaleListDTO dto=mapper.readValue(new File(PATH), SaleListDTO.class);
        System.out.println(dto.toString());
    }

回应:

SaleListDTO(list=[SaleDTO(id=111111, currency=EUR, country=ITA, name=Italy, code=IT), SaleDTO(id=22222, currency=IN, country=INDIA, name=CHENNAI, code=IT)])