【发布时间】:2015-09-30 07:45:50
【问题描述】:
{
"LocalLocationId [id=1]":{
"type":"folderlocation",
"id":{
"type":"locallocationid",
"id":1
},
"parentId":{
"type":"locallocationid",
"id":0
},
"name":"Test",
"accessibleToUser":true,
"defaultLocation":false,
"timezoneId":"Asia/Calcutta",
"children":[]
},
"LocalLocationId [id=0]":{
"type":"folderlocation",
"id":{
"type":"locallocationid",
"id":0
},
"parentId":null,
"name":"Locations",
"accessibleToUser":false,
"defaultLocation":false,
"timezoneId":"Asia/Calcutta",
"children":[{
"type":"locallocationid",
"id":1
}]
},
"allAllowedChildren":[{
"type":"locallocationid",
"id":1
}]
}
如何将上面的字符串反序列化为java对象。
类 im 使用 is
public class Tree {
@SerializedName("allAllowedChildren")
private List<Id> allAllowedChildren;
@SerializedName("LocalLocationId")
private Map<String, LocalLocationId> localLocationId;
public class LocalLocationId {
@SerializedName("type")
private String type;
@SerializedName("name")
private String name;
@SerializedName("accessibleToUser")
private boolean accessibleToUser;
@SerializedName("defaultLocation")
private boolean defaultLocation;
@SerializedName("timezoneId")
private String timezoneId;
@SerializedName("id")
private Id id;
@SerializedName("parentId")
private Id parentId;
@SerializedName("children")
private List<Id> children;
public String getType() {
return type;
}
public String getName() {
return name;
}
public boolean isAccessibleToUser() {
return accessibleToUser;
}
public boolean isDefaultLocation() {
return defaultLocation;
}
public String getTimezoneId() {
return timezoneId;
}
public Id getId() {
return id;
}
public Id getParentId() {
return parentId;
}
public List<Id> getChildren() {
return children;
}
}
public class Id {
private String type;
private Integer id;
public String getType() {
return type;
}
public Integer getId() {
return id;
}
}
public List<Id> getAllAllowedChildren() {
return allAllowedChildren;
}
public Map<String, LocalLocationId> getLocalLocationId() {
return localLocationId;
}
}
【问题讨论】:
-
你遇到了什么错误?
-
将上面的字符串反序列化为java对象是不可能用你现在使用的类来做的。
-
@RyanFung 需要详细说明吗?我猜 OP 不知道为什么会这样。
-
@dotvav.,在
Tree tree = gson.fromJson(locationTree, Tree.class);之后,我得到 LocalLocationId 为null -
@RyanFung 如果需要可以更改树类结构