首页 > 解决方案 > 将 JSONString 反序列化为包含字符串列表的 Java 对象的问题

问题描述

我有一个 JSONString 如下:

{"items":[{"attribute":"grade","values":["AA"]},{"attribute":"subject","values":["mathematics"]}]}

我创建了一个类 Marks 如下:

@Builder
@Getter
@Setter
@ToString
public class Marks {
 @JsonProperty("attribute")
 private String attributeName;
 List<String> values;
}

当我执行以下行时:

 List<Marks> markList = objectMapper.readValue(actionPriorityConfig, new TypeReference<Marks>());

我收到以下异常:

    com.fasterxml.jackson.databind.JsonMappingException: Can not 
   deserialize instance of 
 com.amazon.avstech.autoallocation.model.AttributeAutoAllocNotEligibility out of START_ARRAY token
 at [Source: [{"values":["AA"],"attribute":"grade"},{"values":["mathematics"],"attribute":"subject"}]; line: 1, column: 1]

标签: javajson

解决方案


您可以尝试在属性值声明之前添加 @JsonProperty("values") 吗?

public class Marks {
 @JsonProperty("attribute")
 private String attributeName;

 @JsonProperty("values")
 List<String> values;
}

推荐阅读