java - 将 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]
解决方案
您可以尝试在属性值声明之前添加 @JsonProperty("values") 吗?
public class Marks {
@JsonProperty("attribute")
private String attributeName;
@JsonProperty("values")
List<String> values;
}
推荐阅读
- javascript - Angular:如何在 Angular 中添加/删除文件?
- groovy - 如何在 AST 转换中将 @Delegate 添加到 Groovy 中的字段
- android - 通过 NavigationDrawer 的 PreferenceFragment
- sql-server - where 子句中的最佳搜索字符串
- xamarin - Xamarin.UWP RichTextBlock 悬挂缩进
- shell - 使用 jenkins 执行 shell 脚本部署 ear
- c# - 当进程退出C#时防止程序关闭
- excel - Excel 宏在工作簿之间复制和粘贴
- ios - 在没有动画的情况下一次关闭两个 UIViewController
- ssis - 如何在不使用 sript 任务的情况下读取包含日期值的平面文件的第一行并与 2008 SSIS 包中定义的用户变量进行比较