无法识别的字段,未标记为可忽略

时间:2019-04-03 11:07:58

标签: java serialization deserialization objectmapper

尝试使用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 以从JSON映射 FormMap

1 个答案:

答案 0 :(得分:0)

要启用更改给定JSON属性的目标Java字段的名称,可以使用注释@JsonProperty。就您而言,注释

不是强制将private List<FormMap> formMaps重命名为formMap,而是

@JsonProperty("FormMap")
private List<FormMap> formMaps;

可以使用。