Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 改进了转换嵌套JSON对象的方法';使用Jackson将布尔值转换为映射_Java_Spring_Spring Boot_Jackson_Jackson Databind - Fatal编程技术网

Java 改进了转换嵌套JSON对象的方法';使用Jackson将布尔值转换为映射

Java 改进了转换嵌套JSON对象的方法';使用Jackson将布尔值转换为映射,java,spring,spring-boot,jackson,jackson-databind,Java,Spring,Spring Boot,Jackson,Jackson Databind,我有以下JSON对象 { "donor": "Y", "bloodType": null, "eligibility": { "categoryEligible": false, "suspensionEligible": false, "paidFinesEligible": false, "pointSystemEligible": false, "failedDocuments": [ { "type": "S

我有以下JSON对象

{
  "donor": "Y",
  "bloodType": null,
  "eligibility": {
    "categoryEligible": false,
    "suspensionEligible": false,
    "paidFinesEligible": false,
    "pointSystemEligible": false,
    "failedDocuments": [
      {
        "type": "SOMETHING",
        "reason": "SOMETHING_ELSE"
      }
    ],
    "eligible": false,
  }
}
我正在使用Jackson将其转换为我的域对象。以下是我正在使用的字段:

    private String donor;

    @JsonProperty("eligibility")
    private Eligibility eligibility;

合格性类包含所有这些字段,我不希望所有布尔值都有单独的字段,而是希望有一个映射,其中String是属性名,boolean是值



    @JsonProperty("failedDocuments")
    private List<FailedDocumentsItem> failedDocuments;

    @JsonProperty("eligible")
    private boolean eligible;

    @JsonProperty("donor")
    private boolean donor;


@JsonProperty(“失败文档”)
私人名单失败的文件;
@JsonProperty(“合格”)
私营企业;
@JsonProperty(“捐赠者”)
私人布尔供体;
添加字段(Jackson 2.8+)或方法:

可用于定义逻辑“任意setter”变异体的标记注释——使用非静态双参数方法(属性的第一个参数名称,要设置的第二个值)或字段(类型为
Map
或POJO)——用作从JSON内容中找到的所有其他无法识别的属性的“回退”处理程序

为简洁起见,使用公共字段的示例

public class Test {
    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        Root root = mapper.readValue(new File("test.json"), Root.class);
        System.out.println("donor = " + root.donor);
        System.out.println("flags = " + root.eligibility.flags);
        System.out.println("failedDocuments = " + root.eligibility.failedDocuments);
    }
}
class Root {
    public Boolean realId;
    public String donor;
    public Boolean bloodType;
    public Boolean selectiveServiceCandidate;
    public Eligibility eligibility;
}
class Eligibility {
    @JsonAnySetter
    public Map<String, Boolean> flags = new HashMap<>();
    public List<FailedDocument> failedDocuments;
}
class FailedDocument {
    public String type;
    public String reason;
    @Override
    public String toString() {
        return "FailedDocument[type=" + this.type + ", reason=" + this.reason + "]";
    }
}

感谢您提供这一令人惊叹的解决方案,非常感谢。