When I used Jackson to deserialize the json string ,i often don't want to create all the bean class's properties, and also I just need some json string's field ,other field i don't need. So i often just write some properties in my java class bean which i needed . but when Jackson parsed it it will return as null for the bean field.
1.The java bean class is: GistObject
is:
public class GistObject {
private String id;
}
The jackson Main class code is:
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);
Then the returned GistObject
with field id
is null
, not expected e69fd9f9ef85eb3f30a3b93d2cc9b9b3
. So SOmeone know how to deserialize the json string to java bean without writing all the json string's fields in my java bean class. thanks for your response!
You need to add a setter for your property id into your data class like that:
public static class GistObject {
private String id;
public void setId(String id) { this.id = id; }
}
Or you can use the JsonProperty annotation:
public static class GistObject {
@JsonProperty
private String id;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.