如何将JSON转换为具有特定字段的POJO。

huangapple go评论96阅读模式
英文:

How to convert json to pojo with specific fields

问题

  1. import com.fasterxml.jackson.annotation.JsonProperty;
  2. public class CustomPerson {
  3. @JsonProperty("names")
  4. private Names names;
  5. public CustomPerson() {
  6. }
  7. public String getGivenName() {
  8. return this.names == null ? null : this.names.getGivenName();
  9. }
  10. public String getFamilyName() {
  11. return this.names == null ? null : this.names.getFamilyName();
  12. }
  13. private static class Names {
  14. @JsonProperty("displayName")
  15. private String displayName;
  16. @JsonProperty("familyName")
  17. private String familyName;
  18. @JsonProperty("givenName")
  19. private String givenName;
  20. public String getGivenName() {
  21. return givenName;
  22. }
  23. public String getFamilyName() {
  24. return familyName;
  25. }
  26. }
  27. }

Please make sure that you also have the necessary dependencies, such as Jackson, properly configured in your project to handle JSON serialization and deserialization.

英文:

I tried to convert this json (it's a response from google people api) to pojo. i get null instead of object with data. i think my pojo is not correct and jackson cant convert it

  1. {
  2. "resourceName": "people/113175645456469629209",
  3. "etag": "%EgoBAj0DBgk+NTcuGgQBAgnfvtrUH",
  4. "names": [
  5. {
  6. "metadata": {
  7. "primary": true,
  8. "source": {
  9. "type": "PROFILE",
  10. "id": "113175645456469629209"
  11. }
  12. },
  13. "displayName": "firstName lastName",
  14. "familyName": "lastName",
  15. "givenName": "firstName",
  16. "displayNameLastFirst": "firstName, lastName",
  17. "unstructuredName": "firstName, lastName"
  18. }
  19. ]
  20. }

I want to get only pojo with first name and last name

  1. import com.fasterxml.jackson.annotation.JsonProperty;
  2. import org.springframework.social.google.api.plus.Person;
  3. public class CustomPerson {
  4. @JsonProperty("names")
  5. private CustomPerson.Names names;
  6. public CustomPerson() {
  7. }
  8. public String getGivenName() {
  9. return this.names == null ? null : this.names.givenName;
  10. }
  11. public String getFamilyName() {
  12. return this.names == null ? null : this.names.familyName;
  13. }
  14. private static class Names {
  15. @JsonProperty
  16. private String givenName;
  17. @JsonProperty
  18. private String familyName;
  19. private Names() {
  20. }
  21. }
  22. }

any advice would help me

答案1

得分: 1

  1. public class CustomPerson {
  2. @JsonProperty
  3. private List<CustomPerson.Names> names;
  4. public CustomPerson() {
  5. }
  6. private static class Names {
  7. @JsonProperty
  8. private String givenName;
  9. @JsonProperty
  10. private String familyName;
  11. private Names() {
  12. }
  13. public String getGivenName() {
  14. return this.names == null ? null : this.names.givenName;
  15. }
  16. public String getFamilyName() {
  17. return this.names == null ? null : this.names.familyName;
  18. }
  19. }
  20. }
英文:
  1. public class CustomPerson {
  2. @JsonProperty
  3. private List&lt;CustomPerson.Names&gt; names;
  4. public CustomPerson() {
  5. }
  6. private static class Names {
  7. @JsonProperty
  8. private String givenName;
  9. @JsonProperty
  10. private String familyName;
  11. private Names() {
  12. }
  13. public String getGivenName() {
  14. return this.names == null ? null : this.names.givenName;
  15. }
  16. public String getFamilyName() {
  17. return this.names == null ? null : this.names.familyName;
  18. }
  19. }
  20. }

huangapple
  • 本文由 发表于 2020年9月18日 19:51:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63955242.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定