我是杰克逊的新手。我试图将Json字符串解析为一个对象,但是jackson返回一个包含所有空值的对象。这是我的解析器的代码:
ObjectMapper mapper = new ObjectMapper();
FullTextRetrievalResponse object =
mapper.readValue(response.getBody().getObject().toString(),
FullTextRetrievalResponse.class);
这是我的FullTextRetrievalResponse类:
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"coredata",
"scopus-id",
"scopus-eid",
"link",
"originalText"
})
public class FullTextRetrievalResponse {
@JsonProperty("coredata")
private Coredata coredata;
@JsonProperty("scopus-id")
private String scopusId;
@JsonProperty("scopus-eid")
private String scopusEid;
@JsonProperty("link")
private Link_ link;
@JsonProperty("originalText")
private OriginalText originalText;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("coredata")
public Coredata getCoredata() {
return coredata;
}
@JsonProperty("coredata")
public void setCoredata(Coredata coredata) {
this.coredata = coredata;
}
@JsonProperty("scopus-id")
public String getScopusId() {
return scopusId;
}
@JsonProperty("scopus-id")
public void setScopusId(String scopusId) {
this.scopusId = scopusId;
}
@JsonProperty("scopus-eid")
public String getScopusEid() {
return scopusEid;
}
@JsonProperty("scopus-eid")
public void setScopusEid(String scopusEid) {
this.scopusEid = scopusEid;
}
@JsonProperty("link")
public Link_ getLink() {
return link;
}
@JsonProperty("link")
public void setLink(Link_ link) {
this.link = link;
}
@JsonProperty("originalText")
public OriginalText getOriginalText() {
return originalText;
}
@JsonProperty("originalText")
public void setOriginalText(OriginalText originalText) {
this.originalText = originalText;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
这是JSON的一部分:
{
"full-text-retrieval-response": {
"coredata": {
"prism:url": "http://api.elsevier.com/content/article/pii/S1751157716302140",
"dc:identifier": "doi:10.1016/j.joi.2016.11.002",
"eid": "1-s2.0-S1751157716302140",
"prism:doi": "10.1016/j.joi.2016.11.002",
"pii": "S1751-1577(16)30214-0",
"dc:title": "The specific shapes of gender imbalance in scientific authorships: A network approach ",
"prism:publicationName": "Journal of Informetrics",
"prism:aggregationType": "Journal",
"prism:issn": "17511577",
"prism:coverDate": "2017-02-28",
"prism:coverDisplayDate": "February 2017",
"openaccess": "0",
"openaccessArticle": false,
"openaccessType": null,
"openArchiveArticle": false,
"openaccessSponsorName": null,
"openaccessSponsorType": null,
"openaccessUserLicense": null,
"link": [
{
"@rel": "self",
"@href": "http://api.elsevier.com/content/article/pii/S1751157716302140",
"@_fa": "true"
},
{
"@rel": "scidir",
"@href": "http://www.sciencedirect.com/science/article/pii/S1751157716302140",
"@_fa": "true"
}
]
}
}
}
答案 0 :(得分:1)
问题是你的json对象中有你的所有对象的字段full-text-retrieval-response
,但在你的java类中,FullTextRetrievalResponse
是根。
我认为你有3个选择
full-text-retrieval-response
标签(https://pastebin.com/MtxXSeDW)创建一个具有FullTextRetrievalResponse
实例作为json属性的新类:
public class FullTextRetrievalResponseWrapper {
@JsonProperty("full-text-retrieval-response")
private FullTextRetrievalResponse fullTextRetrievalResponse;
//setters and getters
}
然后使用这个新类进行序列化:mapper.readValue(response.getBody().getObject().toString(),
FullTextRetrievalResponseWrapper .class);
创建一个custon json反序列化器(http://www.baeldung.com/jackson-deserialization),将json对象转换为您的类。
另一个快速提示:如果您将字段定义为json属性(@JsonProperty
),则无需定义@JsonSetter
,@JsonGetter
甚至{{1在setter和getters中。