英文:
How to convert json to pojo with specific fields
问题
import com.fasterxml.jackson.annotation.JsonProperty;
public class CustomPerson {
@JsonProperty("names")
private Names names;
public CustomPerson() {
}
public String getGivenName() {
return this.names == null ? null : this.names.getGivenName();
}
public String getFamilyName() {
return this.names == null ? null : this.names.getFamilyName();
}
private static class Names {
@JsonProperty("displayName")
private String displayName;
@JsonProperty("familyName")
private String familyName;
@JsonProperty("givenName")
private String givenName;
public String getGivenName() {
return givenName;
}
public String getFamilyName() {
return familyName;
}
}
}
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
{
"resourceName": "people/113175645456469629209",
"etag": "%EgoBAj0DBgk+NTcuGgQBAgnfvtrUH",
"names": [
{
"metadata": {
"primary": true,
"source": {
"type": "PROFILE",
"id": "113175645456469629209"
}
},
"displayName": "firstName lastName",
"familyName": "lastName",
"givenName": "firstName",
"displayNameLastFirst": "firstName, lastName",
"unstructuredName": "firstName, lastName"
}
]
}
I want to get only pojo with first name and last name
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.social.google.api.plus.Person;
public class CustomPerson {
@JsonProperty("names")
private CustomPerson.Names names;
public CustomPerson() {
}
public String getGivenName() {
return this.names == null ? null : this.names.givenName;
}
public String getFamilyName() {
return this.names == null ? null : this.names.familyName;
}
private static class Names {
@JsonProperty
private String givenName;
@JsonProperty
private String familyName;
private Names() {
}
}
}
any advice would help me
答案1
得分: 1
public class CustomPerson {
@JsonProperty
private List<CustomPerson.Names> names;
public CustomPerson() {
}
private static class Names {
@JsonProperty
private String givenName;
@JsonProperty
private String familyName;
private Names() {
}
public String getGivenName() {
return this.names == null ? null : this.names.givenName;
}
public String getFamilyName() {
return this.names == null ? null : this.names.familyName;
}
}
}
英文:
public class CustomPerson {
@JsonProperty
private List<CustomPerson.Names> names;
public CustomPerson() {
}
private static class Names {
@JsonProperty
private String givenName;
@JsonProperty
private String familyName;
private Names() {
}
public String getGivenName() {
return this.names == null ? null : this.names.givenName;
}
public String getFamilyName() {
return this.names == null ? null : this.names.familyName;
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论