繁体   English   中英

如何使用杰克逊注释将值json映射到对象java

[英]how to map value json to object java using jackson-annotations

我有一个字符串Json:

{
   "title": "PowerPoint Presentation",
   "author": "Hana",
   "subject": null,
   "keywords": null,
   "created_date": "2016-03-25 15:11:17",
   "modified_date": "2016-03-28 17:27:06",
   "creator": null,
   "producer": "LibreOffice 5.0",
   "pdfversion": null,
   "file_size": 149225,
   "total_page": 24
}

和对象java

public class ContentInfo {

    @JsonProperty("title")
    private String title;

    @JsonProperty("author")
    private String author;

    @JsonProperty("subject")
    private String subject;

    @JsonProperty("keywords")
    private String keywords;

    @JsonProperty("created_date")
    private String createdDate;

    @JsonProperty("modified_date")
    private String modifiedDate;
// (application name that create original file of PDF)

    @JsonProperty("creator")
    private String creator;
// (application name that create PDF)

    @JsonProperty("producer")
    private String producer;

    @JsonProperty("pdfversion")
    private String pdfversion;

    @JsonProperty("file_size")
    private long fileSize;

    @JsonProperty("total_page")
    private long totalPage;

    public ContentInfo() {
    }

    public ContentInfo(String title, String author, String subject, String keywords, String createdDate, String modifiedDate, String creator, String producer, String pdfversion, long fileSize, long totalPage, PageViewSetting page_view_setting) {
        this.title = title;
        this.author = author;
        this.subject = subject;
        this.keywords = keywords;
        this.createdDate = createdDate;
        this.modifiedDate = modifiedDate;
        this.creator = creator;
        this.producer = producer;
        this.pdfversion = pdfversion;
        this.fileSize = fileSize;
        this.totalPage = totalPage;
        this.page_view_setting = page_view_setting;
    }



    public String getTitle() {
        return title;
    }

    public String getAuthor() {
        return author;
    }

    public String getSubject() {
        return subject;
    }

    public String getKeywords() {
        return keywords;
    }

    public String getCreatedDate() {
        return createdDate;
    }

    public String getModifiedDate() {
        return modifiedDate;
    }

    public String getCreator() {
        return creator;
    }

    public String getProducer() {
        return producer;
    }

    public String getPdfversion() {
        return pdfversion;
    }

    public long getFileSize() {
        return fileSize;
    }

    public long getTotalPage() {
        return totalPage;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public void setKeywords(String keywords) {
        this.keywords = keywords;
    }

    public void setCreatedDate(String createdDate) {
        this.createdDate = createdDate;
    }

    public void setModifiedDate(String modifiedDate) {
        this.modifiedDate = modifiedDate;
    }

    public void setCreator(String creator) {
        this.creator = creator;
    }

    public void setProducer(String producer) {
        this.producer = producer;
    }

    public void setPdfversion(String pdfversion) {
        this.pdfversion = pdfversion;
    }

    public void setFileSize(long fileSize) {
        this.fileSize = fileSize;
    }
}

我正在使用下面的代码来映​​射它们:

ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            data = objectMapper.readValue(this.jsonContentInfoData, ContentInfo.class);

但是结果ResponseBody在某些领域是错误的:

"content_info": {
"title": "PowerPoint Presentation"
"author": "Hana"
"subject": null
"keywords": null
"created_date": "2016-03-25 15:11:17"
"creator": null
"producer": "LibreOffice 5.0"
"pdfversion": null
"modified_date": "2016-03-28 17:27:06"
"file_size": 0
"total_page": 0
}

Jackson会根据您字段的访问修饰符以及可用的和正确命名的getter和setter方法进行序列化和反序列化。

您可以覆盖此功能,以确保使用以下命令对所有私有字段进行序列化/反序列化:

mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);

但是,它很适合测试,但不是最佳解决方案。 相反,您实际上应该使用私有字段并使用公共获取程序/设置程序来控制序列化和反序列化过程。

  • 公共获取者使非公共字段可序列化和反序列化

    直觉上,吸气剂也会使私有字段反序列化-因为一旦有了吸气剂,该字段就被视为属性。

  • 公共设置员只能将非公共字段反序列化

在您的代码中,显示获取器/设置器:

  1. setPdfversion不正确:应为setPdfVersion
  2. getTotalPage(long totalPage) -不正确,旨在设置为setTotalPage(long totalPage)

最后,我认为这将有助于将totalPagefileSize的类型从原始long更改为包装对象Long 还要更改这些字段的getter / setter以匹配Long类型。 由于这两个字段都存在问题并且正在使用基元,因此Jackson(或您的版本)似乎无法处理基元。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM