gpt4 book ai didi

java - Jackson反序列化json字符串但bean缺少json字符串的属性/键

转载 作者:行者123 更新时间:2023-12-02 12:38:14 29 4
gpt4 key购买 nike

当我使用 Jackson 反序列化 json 字符串时,我通常不想创建所有 bean 类的属性,而且我只需要一些 json 字符串的字段,其他字段我不需要。所以我经常只在我需要的 java 类 bean 中编写一些属性。但是当 Jackson 解析它时,bean 字段将返回 null。

1.java bean 类是:GistObject 是:

public class GistObject {
private String id;
}
  • jackson 主类代码是:

      String json =
    " {\n" +
    " \"url\": \"https://api.github.com/gists/e69fd9f9ef85eb3f30a3b93d2cc9b9b3\",\n" +
    " \"forks_url\": \"https://api.github.com/gists/e69fd9f9ef85eb3f30a3b93d2cc9b9b3/forks\",\n" +
    " \"commits_url\": \"https://api.github.com/gists/e69fd9f9ef85eb3f30a3b93d2cc9b9b3/commits\",\n" +
    " \"id\": \"e69fd9f9ef85eb3f30a3b93d2cc9b9b3\",\n" +
    " \"git_pull_url\": \"https://gist.github.com/e69fd9f9ef85eb3f30a3b93d2cc9b9b3.git\",\n" +
    " \"git_push_url\": \"https://gist.github.com/e69fd9f9ef85eb3f30a3b93d2cc9b9b3.git\",\n" +
    " \"html_url\": \"https://gist.github.com/e69fd9f9ef85eb3f30a3b93d2cc9b9b3\",\n" +
    " \"files\": {\n" +
    " \"sample template\": {\n" +
    " \"filename\": \"sample template\",\n" +
    " \"type\": \"text/plain\",\n" +
    " \"language\": null,\n" +
    " \"raw_url\": \"https://gist.githubusercontent.com/becauseqa-walter/e69fd9f9ef85eb3f30a3b93d2cc9b9b3/raw/a85b555ce30da1f13aa3b7db3a2756bd64462278/sample%20template\",\n" +
    " \"size\": 877\n" +
    " }\n" +
    " },\n" +
    " \"public\": false,\n" +
    " \"created_at\": \"2017-05-16T13:28:44Z\",\n" +
    " \"updated_at\": \"2017-05-16T13:28:44Z\",\n" +
    " \"description\": \"\",\n" +
    " \"comments\": 0,\n" +
    " \"user\": null,\n" +
    " \"comments_url\": \"https://api.github.com/gists/e69fd9f9ef85eb3f30a3b93d2cc9b9b3/comments\",\n" +
    " \"owner\": {\n" +
    " \"login\": \"becauseqa-walter\",\n" +
    " \"id\": 5029046,\n" +
    " \"avatar_url\": \"https://avatars1.githubusercontent.com/u/5029046?v=3\",\n" +
    " \"gravatar_id\": \"\",\n" +
    " \"url\": \"https://api.github.com/users/becauseqa-walter\",\n" +
    " \"html_url\": \"https://github.com/becauseqa-walter\",\n" +
    " \"followers_url\": \"https://api.github.com/users/becauseqa-walter/followers\",\n" +
    " \"following_url\": \"https://api.github.com/users/becauseqa-walter/following{/other_user}\",\n" +
    " \"gists_url\": \"https://api.github.com/users/becauseqa-walter/gists{/gist_id}\",\n" +
    " \"starred_url\": \"https://api.github.com/users/becauseqa-walter/starred{/owner}{/repo}\",\n" +
    " \"subscriptions_url\": \"https://api.github.com/users/becauseqa-walter/subscriptions\",\n" +
    " \"organizations_url\": \"https://api.github.com/users/becauseqa-walter/orgs\",\n" +
    " \"repos_url\": \"https://api.github.com/users/becauseqa-walter/repos\",\n" +
    " \"events_url\": \"https://api.github.com/users/becauseqa-walter/events{/privacy}\",\n" +
    " \"received_events_url\": \"https://api.github.com/users/becauseqa-walter/received_events\",\n" +
    " \"type\": \"User\",\n" +
    " \"site_admin\": false\n" +
    " },\n" +
    " \"truncated\": false\n" +
    " }";
    ObjectMapper mapper= new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    GistObject gistObject =mapper.readValue(json, GistObject.class);
  • 然后返回的带有字段 idGistObjectnull,而不是预期的 e69fd9f9ef85eb3f30a3b93d2cc9b9b3。所以有人知道如何将 json 字符串反序列化为 java bean,而无需在我的 java bean 类中写入所有 json 字符串的字段。感谢您的回复!

    最佳答案

    您需要将属性 id 的 setter 添加到数据类中,如下所示:

    public static class GistObject {
    private String id;

    public void setId(String id) { this.id = id; }
    }

    或者您可以使用 JsonProperty 注释:

    public static class GistObject {
    @JsonProperty
    private String id;
    }

    关于java - Jackson反序列化json字符串但bean缺少json字符串的属性/键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45053450/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com