2019独角兽企业重金招聘Python工程师标准>>>
在实际开发中,后台经常会传过来相当诡异的JSON数据组合,诡异当然指的是复杂,这种情况一般是就是后台偷懒了,把本来应该放到后台来出来的逻辑全部发送给前端来处理,懒惰是原罪啊。
当然,这一篇的目的不是为了吐槽,当我们必须处理后台发来的数据时,跪着也得搞完啊!所以,今天就罗列几组数据当例子,分析一下前端应该如何处理。
第一组(高能):
[
{
ParentId: null,
Name: "XX集团",
Id: 8,
Code: "Dept-jituan1",
Leader: null,
Type: "Department",
ChildDepartmentCount: 1,
UserInfoCount: 5,
Children: [
{
ParentId: 8,
Name: "XX有限公司",
Id: 10,
Code: "Dept-gongsi1",
Leader: null,
Type: "Department",
ChildDepartmentCount: 2,
UserInfoCount: 5,
Children: [
{
ParentId: 10,
Name: "项目部",
Id: 11,
Code: "Dept-bumen1",
Leader: null,
Type: "Department",
ChildDepartmentCount: 2,
UserInfoCount: 4,
Children: [
{
ParentId: 11,
Name: "项目一部",
Id: 18,
Code: "Dept-bumen2",
Leader: {
ParentId: 18,
Name: "徐XX",
Id: 41391,
Code: "User-xumaojun",
Type: "UserInfo",
Children: null,
Position: "",
DepartmentName: "项目一部",
Email: null,
Phone: null,
Gender: "Man",
Address: null,
FaceUrl: null
},
Type: "Department",
ChildDepartmentCount: 0,
UserInfoCount: 2,
Children: [
{
ParentId: 18,
Name: "徐XX",
Id: 41391,
Code: "User-xumaojun",
Type: "UserInfo",
Children: null,
Position: "",
DepartmentName: "项目一部",
Email: null,
Phone: null,
Gender: "Man",
Address: null,
FaceUrl: null
},
{
ParentId: 18,
Name: "潘XX",
Id: 41392,
Code: "User-panzhaohui",
Type: "UserInfo",
Children: null,
Position: "",
DepartmentName: "项目一部",
Email: null,
Phone: null,
Gender: "Man",
Address: null,
FaceUrl: null
}
]
},
{
ParentId: 11,
Name: "项目二部",
Id: 19,
Code: "Dept-bumen3",
Leader: {
ParentId: 19,
Name: "wangXX",
Id: 41393,
Code: "User-zouxichao",
Type: "UserInfo",
Children: null,
Position: "",
DepartmentName: "项目二部",
Email: null,
Phone: null,
Gender: "Man",
Address: null,
FaceUrl: null
},
Type: "Department",
ChildDepartmentCount: 0,
UserInfoCount: 2,
Children: [
{
ParentId: 19,
Name: "James",
Id: 5,
Code: "User-mawentai",
Type: "UserInfo",
Children: null,
Position: "Manager",
DepartmentName: "项目二部",
Email: null,
Phone: null,
Gender: null,
Address: null,
FaceUrl: null
},
{
ParentId: 19,
Name: "wangXX",
Id: 41393,
Code: "User-zouxichao",
Type: "UserInfo",
Children: null,
Position: "",
DepartmentName: "项目二部",
Email: null,
Phone: null,
Gender: "Man",
Address: null,
FaceUrl: null
}
]
}
]
},
{
ParentId: 10,
Name: "市场营销部",
Id: 17,
Code: "Dept-1231",
Leader: null,
Type: "Department",
ChildDepartmentCount: 0,
UserInfoCount: 1,
Children: [
{
ParentId: 17,
Name: "李XX",
Id: 41394,
Code: "User-gemei",
Type: "UserInfo",
Children: null,
Position: "",
DepartmentName: "市场营销部",
Email: null,
Phone: null,
Gender: "Woman",
Address: null,
FaceUrl: null
}
]
}
]
}
]
}
]
推荐Chrome下的JSONView插件,可以清晰看出JSON对象的结构,看到没,这是第一组复杂数据,我大致数了下有差不多五层嵌套,实际上这是需要在五个页面展示的数据,其实分析透了会发现这几组数据有个共同点,JSON对象的最小单元在"Children"这个Tag里,所以这个Tag下的每个对象可以抽象出来作为PersonBean,他有13个变量,而"Children"的上一级则是另一个Bean,它有8个变量,当然我们可以看到跟"Children"同级的"header"也是一个PersonBean,所以我们单独拿出来让它和"Children"同级。因为我们是在一个页面获取的所有数据,并没有必要在一个页面处理所有的数据,所以第一层的"Children"我们选择,把它单独保留下来,转向下个页面的时候,把组成它的字符串传过去,让下个页面来处理,减轻当前页面的负担。于是,形成如下的数据结构:
public class CommonBean implements Serializable{
private String Name;
private int ParentId;
private int ChildDepartmentCount;
private int UserInfoCount;
private String Type;
private int Id;
private String Code;
//Children数组
private String array;
//leader信息
private PersonContactsBean leader;
public void setLeader(PersonContactsBean leader) {
this.leader = leader;
}
public PersonContactsBean getLeader() {
return leader;
}
public void setArray(String array) {
this.array = array;
}
public String getArray() {
return array;
}
public void setName(String Name) {
this.Name = Name;
}
public void setParentId(int ParentId) {
this.ParentId = ParentId;
}
public void setChildDepartmentCount(int ChildDepartmentCount) {
this.ChildDepartmentCount = ChildDepartmentCount;
}
public void setUserInfoCount(int UserInfoCount) {
this.UserInfoCount = UserInfoCount;
}
public void setType(String Type) {
this.Type = Type;
}
public void setId(int Id) {
this.Id = Id;
}
public void setCode(String Code) {
this.Code = Code;
}
public String getName() {
return Name;
}
public int getParentId() {
return ParentId;
}
public int getChildDepartmentCount() {
return ChildDepartmentCount;
}
public int getUserInfoCount() {
return UserInfoCount;
}
public String getType() {
return Type;
}
public int getId() {
return Id;
}
public String getCode() {
return Code;
}
@Override
public String toString() {
return getType()+getCode();
}
}