【发布时间】: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;
}
-
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 字段为id 是null,而不是预期的e69fd9f9ef85eb3f30a3b93d2cc9b9b3。
所以有人知道如何将 json 字符串反序列化为 java bean,而无需在我的 java bean 类中写入所有 json 字符串的字段。
感谢您的回复!
【问题讨论】: