【发布时间】:2017-07-28 23:05:47
【问题描述】:
我正在调用 Web 服务,它可以有 2 种类型的 json 对象作为响应。现在有时我会得到字符串类型的键profile,有时它可能具有类型为“ProfileSubObject”的相同键。那么如何处理这种情况呢?以下是我的两种类型的对象。我正在使用 Jackson 库来解析 json。
1.)
{
"data": [
{
"profession": "iOS Developer",
"thanks": {
"count": 5
},
"profile": "test"
}
]
}
2.)
{
"data": [
{
"profession": "iOS Developer",
"thanks": {
"count": 5
},
"profile": {
"val1":"test1",
"val2":"test2"
}
}
]
}
Key profile 有 2 种不同类型的基于 web 服务调用的对象。
以下是我的数据类结构。
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class DataObject {
@JsonProperty("profession")
private String profession;
@JsonProperty("profile")
private ProfileObject profile;
@JsonProperty("thanks")
private ThanksObject thanks;
public String getProfession() {
return profession;
}
public ThanksObject getThanks() {
return thanks;
}
public ProfileObject getProfile() {
return profile;
}
}
Profile 类如下。
public class ProfileObject {
ProfileObject(){
}
ProfileObject(ProfileSubObject profileSubObject){
this.profileSubObject= profileSubObject;
}
ProfileObject(String profile){
this.profile= profile;
}
private ProfileSubObject profileSubObject;
private String profile;
public ProfileSubObject getProfileSubObject() {
return profileSubObject;
}
}
现在,当我解析我的对象时,ProfileObject 始终为空。我希望它根据proifle 键数据类型进行解析。
谁能帮我解析一下?
【问题讨论】:
-
你的意思是在
"profile": "test"的情况下它是一个子配置文件吗? -
@Sharon Ben Asher 编辑了我的问题