当我使用Jackson来反序列化json字符串时,我经常不想创建所有bean类的属性,而且我只需要一些json字符串字段,其他字段我不需要。所以我经常在我需要的java类bean中编写一些属性。但是当Jackson解析它时,它将返回bean字段的空值。
1. java bean类是:GistObject
是:
public class GistObject {
private String id;
}
杰克逊主类代码是:
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
。
所以SOmeone知道如何将json字符串反序列化为java bean而不在我的java bean类中编写所有json字符串的字段。
谢谢你的回复!
答案 0 :(得分:1)
您需要将属性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;
}