【发布时间】:2017-08-11 07:53:11
【问题描述】:
我正在使用改造 2 获取 JSON,如果键具有值,则它携带对象,否则为空数组。例如:
如果键,即 address 有值,则返回对象
{
"student": {
"name": "Some name",
"address": {
"house": "5",
"road": "3"
}
}
}
如果键即 address 没有任何值,则返回空数组
{
"student": {
"name": "Some name",
"address": []
}
}
在我的 POJO 类中,我将 Address 类类型设置为对象,以便改造可以解析 JSON。
public class Student {
@SerializedName("name")
@Expose
private String name;
@SerializedName("address")
@Expose
private Object address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Object getAddress() {
return address;
}
public void setAddress(Object address) {
this.address = address;
}
}
现在如何检查 address 类型是对象还是数组?
我尝试过使用 isArray(),但没有找到结果。
if(obj.getclass().isArray())
提前致谢。
【问题讨论】:
-
你可以这样做
(obj instanceof JSONObject)或(obj instanceof JSONArray) -
检查这是否对您有帮助 stackoverflow.com/a/13100642/2116420
标签: java android json retrofit2 jsonschema2pojo