一、场景介绍
在工作过程中,需要去调另一个系统的接口,获取机构数据,在得到机构数据之后,需要对数据进行遍历然后根据条件获得自己想要的数据,下面我们看代码。
二、代码示例
我们先通过postman来看一下返回的数据结构,可以看出数据关系是父子级,父级有一个children属性,但是最低级没有children属性,通过这个关系我们可以对其遍历,上代码
"code": 0, "msg": "success", "time": "2021-06-03 17:46:58", "data": [ { "auditOrg": "", "purpose": "1", "orgCname": "赤峰市", "description": "", "invalidFlag": "1", "oid": "", "label": "赤峰市", "deptType": "", "orgFunction": "0", "ifSchool": "", "centerManage": "", "affiliation": "", "children": [ { "auditOrg": "000000", "purpose": "1", "description": "", "invalidFlag": "1", "oid": "a69f0dc8b8264a098a3b4dc2b2f3f230", "label": "市本级", "deptType": "0104", "orgFunction": "1", "ifSchool": "0", "centerManage": "0", "affiliation": "", "children": [ { "auditOrg": "000000", "purpose": "1", "description": "", "invalidFlag": "1", "oid": "81f01a7c56484d9c8df273151759a225", "label": "市纪委监委", "deptType": "0105", "orgFunction": "1", "ifSchool": "0", "centerManage": "0", "affiliation": "", "children": [ { "auditOrg": "000000", "purpose": "1", "description": "", "invalidFlag": "1", "oid": "5d9a15340d0848fca5e0bbb82d774577", "label": "内设机构", "deptType": "0105", "orgFunction": "1", "ifSchool": "0", "centerManage": "0", "affiliation": "", "children": [ { "auditOrg": "000000", "purpose": "1", "description": "", "invalidFlag": "1", "oid": "8005d9ee8abe4c6abad3a86423081967", "label": "党风政风监督室", "deptType": "0105", "orgFunction": "1", "ifSchool": "0", "centerManage": "0", "affiliation": "", "orgCode": "090101002000", "mappingOrgCode": "DFZFJDS00000", "orgShortName": "", "userType": "1", "value": "{\"value\":\"090101002000\",\"label\":\"党风政风监督室\"}" }, { "auditOrg": "000000", "purpose": "1", "description": "", "invalidFlag": "1", "oid": "e56ae12819c7438fbb4d6c96142c95b0", "label": "第二监督检查室", "deptType": "0105", "orgFunction": "1", "ifSchool": "0", "centerManage": "0", "affiliation": "", "orgCode": "090101001000", "mappingOrgCode": "DEJD00000000", "orgShortName": "", "userType": "1", "value": "{\"value\":\"090101001000\",\"label\":\"第二监督检查室\"}" }, { "auditOrg": "000000", "purpose": "1", "description": "", "invalidFlag": "1", "oid": "de486adcf2a4449cad2c9ec0e4755761", "label": "第三监督检查室", "deptType": "0105", "orgFunction": "1", "ifSchool": "0", "centerManage": "0", "affiliation": "", "orgCode": "090101003000", "mappingOrgCode": "DSJDJCS00000", "orgShortName": "", "userType": "1", "value": "{\"value\":\"090101003000\",\"label\":\"第三监督检查室\"}" }, { "auditOrg": "000000", "purpose": "1", "description": "", "invalidFlag": "1", "oid": "69be1c774c44482eaf672c7d50bf5e28", "label": "第四监督检查室", "deptType": "0105", "orgFunction": "1", "ifSchool": "0", "centerManage": "0", "affiliation": "", "orgCode": "090101004000", "mappingOrgCode": "DSIJDJCS0000", "orgShortName": "", "userType": "1", "value": "{\"value\":\"090101004000\",\"label\":\"第四监督检查室\"}" }, { "auditOrg": "000000", "purpose": "1", "description": "", "invalidFlag": "1", "oid": "d224c358b29543a09e5c6373fa72ef8d", "label": "第六监督检查室", "deptType": "0105", "orgFunction": "1", "ifSchool": "0", "centerManage": "0", "affiliation": "", "orgCode": "090101005000", "mappingOrgCode": "DLJDJCS00000", "orgShortName": "", "userType": "1", "value": "{\"value\":\"090101005000\",\"label\":\"第六监督检查室\"}" } ], "orgCode": "090101000000", "mappingOrgCode": "NSJG00000000", "orgShortName": "", "userType": "1", "value": "{\"value\":\"090101000000\",\"label\":\"内设机构\"}" },
三、代码实现
private List<Map<String, Object>> getDataList(List<Map<String, Object>> data, List<Map<String, Object>> list) { //0102 0103 0104 开始遍历 for (int i = 0; i < data.size(); i++) { //先筛选条件 符合添加到list里面 if (data.get(i).get("deptType").equals(regionTypeUtil.QXQ_LEVEL) || data.get(i).get("deptType").equals(regionTypeUtil.SQZBM_LEVEL)) { Map map = new HashMap<String, Object>(); map.put("orgCode", data.get(i).get("orgCode")); map.put("orgName", data.get(i).get("label")); list.add(map); } //如果children不为空,递归调用 if (data.get(i).get("children") != null) { List<Map<String, Object>> datac = (List<Map<String, Object>>) data.get(i).get("children"); getDataList(datac,list); } } return list; }