我正在尝试使用 ObjectMapper 反序列化 Json,但无法反序列化日期值

问题描述

通过将所有日期类型替换为字符串来解决

这是一个来自漫威漫画 API 的 JSON,我正在尝试反序列化并且我得到了

"com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance
of `com.example.demo.json2csharp.Date` (although at least one Creator exists): 
no String-argument constructor/factory method to deserialize from String value 
('2019-11-07T08:46:15-0500')"

,JSON 示例:

{
  "code": 200,"status": "Ok","copyright": "© 2021 MARVEL","attributionText": "Data provided by Marvel. © 2021 MARVEL","attributionHTML": "<a href=\"http://marvel.com\">Data provided by Marvel. © 2021 MARVEL</a>","etag": "f712574873e89d0505dc68a908170fb7970d2f13","data": {
    "offset": 0,"limit": 20,"total": 1,"count": 1,"results": [
      {
        "id": 82967,"digitalId": 0,"title": "Marvel Previews (2017)","issueNumber": 0,"variantDescription": "","description": null,"modified": "2019-11-07T08:46:15-0500","isbn": "","upc": "75960608839302811","diamondCode": "","ean": "","issn": "","format": "","pageCount": 112,"textObjects": [
          
        ],"resourceURI": "http://gateway.marvel.com/v1/public/comics/82967","urls": [
          {
            "type": "detail","url": "http://marvel.com/comics/issue/82967/marvel_previews_2017?utm_campaign=apiRef&utm_source=9a0517af422c1dfbe132dbaf086fa7f7"
          }
        ],"series": {
          "resourceURI": "http://gateway.marvel.com/v1/public/series/23665","name": "Marvel Previews (2017 - Present)"
        },"variants": [
          {
            "resourceURI": "http://gateway.marvel.com/v1/public/comics/82965","name": "Marvel Previews (2017)"
          },{
            "resourceURI": "http://gateway.marvel.com/v1/public/comics/82970","name": "Marvel Previews (2017)"
          }
        ],"collections": [
          
        ],"collectedissues": [
          
        ],"dates": [
          {
            "type": "onsaleDate","date": "2099-10-30T00:00:00-0500"
          },{
            "type": "focDate","date": "2019-10-07T00:00:00-0400"
          }
        ],"prices": [
          {
            "type": "printPrice","price": 0
          }
        ],"thumbnail": {
          "path": "http://i.annihil.us/u/prod/marvel/i/mg/b/40/image_not_available","extension": "jpg"
        },"images": [
          
        ],"creators": {
          "available": 1,"collectionURI": "http://gateway.marvel.com/v1/public/comics/82967/creators","items": [
            {
              "resourceURI": "http://gateway.marvel.com/v1/public/creators/10021","name": "Jim Nausedas","role": "editor"
            }
          ],"returned": 1
        },"characters": {
          "available": 0,"collectionURI": "http://gateway.marvel.com/v1/public/comics/82967/characters","items": [
            
          ],"returned": 0
        },"stories": {
          "available": 2,"collectionURI": "http://gateway.marvel.com/v1/public/comics/82967/stories","items": [
            {
              "resourceURI": "http://gateway.marvel.com/v1/public/stories/183698","name": "cover from Marvel Previews (2017)","type": "cover"
            },{
              "resourceURI": "http://gateway.marvel.com/v1/public/stories/183699","name": "story from Marvel Previews (2017)","type": "interiorStory"
            }
          ],"returned": 2
        },"events": {
          "available": 0,"collectionURI": "http://gateway.marvel.com/v1/public/comics/82967/events","returned": 0
        }
      }
    ]
  }
}

与 ObjectMapper 中使用的所有其他类一样,我的 Date 类是在 https://json2csharp.com/json-to-pojo 生成的:

public class Date {
    @JsonProperty("type")
    public String getType() {
        return this.type;
    }

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

    String type;

    @JsonProperty("date")
    public Date getDate() {
        return this.date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
    Date date;


    public Date() {
    }
}

调用mapper的方法是这样的:

@PostMapping
    public Root getComic(@RequestBody ComicPostRequestBody comicPostRequestBody) throws NoSuchAlgorithmException,URISyntaxException,IOException {
        URI uri = comicService.makeUrl(comicPostRequestBody.getComicId());
        String json = client.getComic(uri);
        ObjectMapper mapper = new ObjectMapper();
        Root comicWrapper = mapper.readValue(json,Root.class);
        return comicWrapper;
    }

Root 是包含所有属性的类

public class Root {
    int code;
    String status;
    String copyright;
    String attributionText;
    String attributionHTML;
    String etag;
    Data data;

    @JsonProperty("code")
    public int getCode() {
        return this.code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    @JsonProperty("status")
    public String getStatus() {
        return this.status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @JsonProperty("copyright")
    public String getcopyright() {
        return this.copyright;
    }

    public void setcopyright(String copyright) {
        this.copyright = copyright;
    }

    @JsonProperty("attributionText")
    public String getAttributionText() {
        return this.attributionText;
    }

    public void setAttributionText(String attributionText) {
        this.attributionText = attributionText;
    }

    @JsonProperty("attributionHTML")
    public String getAttributionHTML() {
        return this.attributionHTML;
    }

    public void setAttributionHTML(String attributionHTML) {
        this.attributionHTML = attributionHTML;
    }

    @JsonProperty("etag")
    public String getEtag() {
        return this.etag;
    }

    public void setEtag(String etag) {
        this.etag = etag;
    }

    @JsonProperty("data")
    public Data getData() {
        return this.data;
    }

    public void setData(Data data) {
        this.data = data;
    }

    public Root() {
    }
}

我的最终目标是创作一部漫画:

results.id
results.title
results.description
results.isbn
results.price

我是编程新手,我发现这样做的方法是将所有 json 反序列化为一个 Root,这样我就可以获得所有这些属性,例如 root.getData().getResults() 如果有一种简单的方法可以解决这个问题或反序列化这个 JSON,我会很高兴知道

谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)