如何解决如何使用特定字段将json转换为pojo
我试图将此json(这是Google People API的响应)转换为pojo。我得到null而不是带有数据的对象。我认为我的pojo不正确,杰克逊无法将其转换
{
"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"
}
]
}
我只想获得名字和姓氏为首的
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() {
}
}
}
任何建议都会帮助我
解决方法
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;
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。