【问题标题】:Jackson deserialize json string but bean missing json string's property/key杰克逊反序列化 json 字符串但 bean 缺少 json 字符串的属性/键
【发布时间】:2017-12-16 15:10:03
【问题描述】:

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

1.java bean类为:GistObject为:

public class GistObject {
   private String id;
}
  1. 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);
    

那么返回的GistObject 字段为idnull,而不是预期的e69fd9f9ef85eb3f30a3b93d2cc9b9b3。 所以有人知道如何将 json 字符串反序列化为 java bean,而无需在我的 java bean 类中写入所有 json 字符串的字段。 感谢您的回复!

【问题讨论】:

    标签: java json jackson


    【解决方案1】:

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

    public static class GistObject {
        private String id;
    
        public void setId(String id) { this.id = id; }
    }
    

    或者你可以使用JsonProperty注解:

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

    【讨论】:

    • 谢谢@Prim,这是我的错。我忘记了 te setter/getter 。
    猜你喜欢
    • 2020-12-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 2016-10-28
    • 2018-10-19
    相关资源
    最近更新 更多