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

问题描述 投票:0回答:1

尝试使用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;
}

我有这两个类,我想使用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

java serialization deserialization objectmapper
1个回答
0
投票

要为给定的JSON属性更改目标Java字段的名称,可以使用注释@JsonProperty。在你的情况下,而不是被迫将private List<FormMap> formMaps重命名为formMap,注释

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

可以使用。

© www.soinside.com 2019 - 2024. All rights reserved.