java - 无法识别的字段,未标记为可忽略
问题描述
尝试使用 ObjectMapper 填充对象列表的字段时出现错误
public class FormMap {
private String id;
private String strengthRouteFormId;
private String fdaid;
private String formName;
private String formDescription;
private String ncitQuantityUnitTermId;
private String ncitStrengthFormTermId;
private String ncitId;
private String ncitSubsetCode;
private String ncpdpSubsetPreferredTerm;
private String ncitCode;
private String ncpdpPreferredTerm;
private String ncitPreferredTerm;
private String ncitDefinition;
private StrengthRouteForm strengthRouteForm;
}
public class StrengthRouteForm {
private String id;
private String medicationId;
private String strength;
private String routeName;
private String gsForm;
private String alternativeFormName;
private String clinicalDoseFormName;
private String normalizedFormName;
private String topLevelDoseFormName;
private List<FormMap> formMaps;
}
我有这 2 个类,我想使用 ObjectMapper 和 Json 创建 StrengthRouteForm 列表
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
List<StrengthRouteForm> strengthRouteForms = null;
strengthRouteForms = Arrays.asList(objectMapper.readValue("string json", StrengthRouteForm[].class));
我得到这个错误:
Unrecognized field "FormMap" (class com.cgm.us.ais.core.model.StrengthRouteForm), not marked as ignorable
JSON 可能如下所示:
"StrengthRouteForm": [
{
"Strength": "0.25",
"RouteName": "Oral",
"GSForm": "Oral tablet",
"AlternativeFormName": "Oral Solid",
"ClinicalDoseFormName": "Oral tablet",
"NormalizedFormName": "Oral tablet",
"TopLevelDoseFormName": "Oral preparations - solid forms",
"FormMap": [
{
"FDAID": "500",
"FormName": "Tab",
"FormDescription": "Tablet",
"NCItQuantityUnitTermID": "106",
"NCItStrengthFormTermID": "447",
"NCItId": "106",
"NCItSubsetCode": "C89510",
"NCPDPSubsetPreferredTerm": "NCPDP QuantityUnitOfMeasure Terminology",
"NCItCode": "C48542",
"NCPDPPreferredTerm": "Tablet",
"NCItPreferredTerm": "Tablet Dosing Unit",
"NCItDefinition": "A dosing unit equal to the amount of active ingredient(s) contained in a tablet."
},
{
"FDAID": "500",
"FormName": "Tab",
"FormDescription": "Tablet",
"NCItQuantityUnitTermID": "106",
"NCItStrengthFormTermID": "447",
"NCItId": "447",
"NCItSubsetCode": "C89508",
"NCPDPSubsetPreferredTerm": "NCPDP StrengthForm Terminology",
"NCItCode": "C42998",
"NCPDPPreferredTerm": "Tablet",
"NCItPreferredTerm": "Tablet Dosage Form",
"NCItDefinition": "A solid composed of a mixture of that active and/or inert ingredient(s) are pressed or compacted together, usually in the form of a relatively flat and round, square or oval shape."
}
]
}
]
我的问题是:如何制作此列表:formMaps to map FormMap from JSON
解决方案
要启用更改给定 JSON 属性的目标 Java 字段的名称,@JsonProperty
可以使用注释。在您的情况下,注释不是被迫重命名private List<FormMap> formMaps
为formMap
@JsonProperty("FormMap")
private List<FormMap> formMaps;
可以使用。
推荐阅读
- spring-boot - Spring 服务器不以执行器依赖项启动
- json - Scala,Circe - 如何检查 json 是否包含具有不同值的键?
- spring-boot - 如果我的其他微服务不是反应式的,我应该使用 Spring Cloud Gateway
- javascript - 使用 Cypress.Promise 在测试中获取列表值以进行比较和搜索
- c# - Azure ServiceBus FaultTolerantAmqpObject`1' 异常
- jquery - 无法在表单提交之前禁用遮罩功能
- javascript - 为什么我的表格不按数字顺序对我的 div 变量进行排序?
- javascript - 是否可以在 JS 上使用字符串创建哈希码?
- javascript - 使用 SMTPJS 和 Mailgun 发送电子邮件失败
- javascript - 使用 CRON NodeJS 和第三方 API,如何避免速率限制?