将具有多个未知字段的 JSON 映射到 Java POJO

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

我正在尝试将以下 JSON 映射到 java POJO。下面是 JSON。

{
  "customObject": {
    "value": {
      "config-1": {
        "configurationName": {
          "de": "Das ist ein Text",
          "en": "Hospitalization Cover"
        },
        "key": "hospitalization-cover",
        "mandatory": false,
        "type": "options",
        "options": {
          "1Lakh": {
            "label": "1 Lakh",
            "priceType": "fixed",
            "price": "200",
            "default": "true"
          },
          "2Lakh": {
            "label": "2 Lakh",
            "priceType": "fixed",
            "price": "400"
          },
          "3Lakh": {
            "label": "3 Lakh",
            "priceType": "fixed",
            "price": "450"
          },
          "4Lakh": {
            "label": "4 Lakh",
            "priceType": "fixed",
            "price": "500"
          },
          "5Lakh": {
            "label": "5 Lakh",
            "priceType": "fixed",
            "price": "550"
          },
          "6Lakh": {
            "label": "6 Lakh",
            "priceType": "fixed",
            "price": "650"
          },
          "7Lakh": {
            "label": "7 Lakh",
            "priceType": "fixed",
            "price": "780"
          }
        }
      },
      "config-2": {
        "configurationName": {
          "de": "Das ist ein Text",
          "en": "OPD Cover"
        },
        "key": "opd-cover",
        "mandatory": false,
        "type": "number",
        "options": {
          "OPDCover10k": {
            "label": "10,000",
            "priceType": "calculated",
            "priceFactor": 0.2,
            "default": "true"
          },
          "OPDCover15k": {
            "label": "15,000",
            "priceType": "calculated",
            "priceFactor": 0.27
          }
        }
      }
    }
  }
}

我创建了下面的 POJO 来映射它。

package com.edu.epam.graphQLSearch.custom.graphlQLqueries;

public class GqCustomObject {

    private GqConfigCustomPOValue customObject;

    public GqConfigCustomPOValue getCustomObject() {
        return customObject;
    }

    public void setCustomObject(GqConfigCustomPOValue customObject) {
        this.customObject = customObject;
    }
}

上面的 POJO 工作正常。 下面是第二个POJO。

package com.edu.epam.graphQLSearch.custom.graphlQLqueries;

public class GqConfigCustomPOValue {


    public GqCustomPOCAList value;

    public GqCustomPOCAList getValue() {
        return value;
    }

    public void setValue(GqCustomPOCAList value) {
        this.value = value;
    }
}

下面是第三个POJO

package com.edu.epam.graphQLSearch.custom.graphlQLqueries;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import java.util.List;


public class GqCustomPOCAList {


    public List<GqCustomPOConfigAttributes> config;

    public List<GqCustomPOConfigAttributes> getConfig() {
        return config;
    }

    public void setConfig(List<GqCustomPOConfigAttributes> config) {
        this.config = config;
    }
}

下面是第四个POJO

package com.edu.epam.graphQLSearch.custom.graphlQLqueries;

public class GqCustomPOConfigAttributes {

    private String configurationName;
    private String key;
    private boolean mandatory;
    private String type;
    private Object options;

    public String getConfigurationName() {
        return configurationName;
    }

    public void setConfigurationName(String configurationName) {
        this.configurationName = configurationName;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public boolean isMandatory() {
        return mandatory;
    }

    public void setMandatory(boolean mandatory) {
        this.mandatory = mandatory;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Object getOptions() {
        return options;
    }

    public void setOptions(Object options) {
        this.options = options;
    }
}

我遇到了 POJO 的问题

GqCustomPOCAList
。在这里,我无法定义 JSON 中提到的键“config-1”。

当我将其定义为

public List<GqCustomPOConfigAttributes> config;
时,我收到错误

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "config-1" (class com.edu.epam.graphQLSearch.custom.graphlQLqueries.GqCustomPOCAList), not marked as ignorable (one known property: "config"])
 at [Source: (String)"{"customObject":{"value":{"config-1":{"configurationName":{"de":"Das ist ein Text","en":"Hospitalization Cover"},"key":"hospitalization-cover","mandatory":false,"type":"options","options":{"1Lakh":{"label":"1 Lakh","priceType":"fixed","price":"200","default":"true"},"2Lakh":{"label":"2 Lakh","priceType":"fixed","price":"400"},"3Lakh":{"label":"3 Lakh","priceType":"fixed","price":"450"},"4Lakh":{"label":"4 Lakh","priceType":"fixed","price":"500"},"5Lakh":{"label":"5 Lakh","priceType":"fixed","pri"[truncated 447 chars]; line: 1, column: 39] (through reference chain: com.edu.epam.graphQLSearch.custom.graphlQLqueries.GqCustomObject["customObject"]->com.edu.epam.graphQLSearch.custom.graphlQLqueries.GqConfigCustomPOValue["value"]->com.edu.epam.graphQLSearch.custom.graphlQLqueries.GqCustomPOCAList["config-1"])
    at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61)

我无法在java类中定义如下变量

public List<GqCustomPOConfigAttributes> config-1;

有没有更好的方法来处理这个问题?

java jackson pojo jsonschema2pojo
1个回答
0
投票

当您有多个未知字段时,解析它们的最佳方法是使用

@JsonAnySetter
注释。此注释允许您在您选择的特定字段中添加未知的键值对(通常是
Map
)。

这是一个解析 json 的工作演示,位于

OneCompiler

以下是代码中的建议/更正列表:

class GqCustomPOCAList {

    // 1. Do not use a List to parse an unknown number of configs,
    // use instead the @JsonAnyGetter annotation and a Map to add every config with their name as the key (config-1, config-2, etc.)
    public Map<String, GqCustomPOConfigAttributes> config;

    // 2. initializing the Map so that it can be used by the method marked with @JsonAnySetter
    public GqCustomPOCAList(){
        config = new HashMap<>();
    }

    // 3. Method to add a generic config to the Map
    @JsonAnySetter
    public void addConfig(String cofigName, GqCustomPOConfigAttributes value){
        config.put(cofigName, value);
    }

    //... other methods ...
}
class GqCustomPOConfigAttributes {

    // 4. configurationName appears to be a Map<String, String> within your Json, not a String
    private Map<String, String> configurationName;
    private String key;
    private boolean mandatory;
    private String type;

    // 5. Do not use a generic Object to represent your option, define a class Option with the possible fields.
    // 6. Again, if the possible options are unknown, use a Map<String, Option> and the @JsonAnySetter annotation
    private Map<String, Option> options;

    // 7. initializing the Map so that it can be used by the method marked with @JsonAnySetter
    public GqCustomPOConfigAttributes(){
        configurationName = new HashMap<>();    
    }

    // 8. Method to add a generic option to the Map
    @JsonAnySetter
    public void addOption(String optionName, Option option){
        options.put(optionName, option);
    }

    // ... other methods ...
}
class Option {
    private String label;
    private String priceType;
    private BigDecimal price;
    private BigDecimal priceFactor;

    // 9. the 'default' property must be renamed as default is a java keyword,
    // use @JsonProperty to bind it to the field 'default' within the Json
    @JsonProperty("default")
    private boolean isDefault;

    // ... other methods ...
}
© www.soinside.com 2019 - 2024. All rights reserved.