public class RecursionTest {
public String updateCategrotyRel(Integer idMerchant)
throws Exception{
String str = "[{\"idOpeCate\":1,\"name\":\"手机\",\"parentId\":0,"
+ "\"children\":[{\"idOpeCate\":2,\"name\":\"苹果\",\"parentId\":1},"
+ "{\"idOpeCate\":3,\"name\":\"小米\",\"parentId\":1}]},"
+ "{\"idOpeCate\":4,\"name\":\"电脑\",\"parentId\":0}]";
//根据idMerchant获取表中已经勾选的ID
List<Integer> checkedIds = xxService.getCheckedIds(idMerchant);
str = this.SetCatChecked(str,checkedIds);
return str;
}
private String SetCatChecked(String str, List<Integer> checkedIds)
throws JsonParseException, JsonMappingException, IOException {
List<Map> strList = JsonUtil.jsonToObjectList(str, Map.class);
recursionOpeCate(checkedIds,strList);
str = JsonUtil.objectListToString(strList);
return str;
}
//递归设置是否选中
private void recursionOpeCate(List<Integer> checkedIds, List<Map> strList) {
for(int i = 0;i < (strList != null && strList.size() > 0
? strList.size() : 0);i++){
if(strList.get(i).get("children") != null){
this.recursionOpeCate(checkedIds, (List<Map>)strList.get(i).get("children"));
}
for(int j = 0;j < (checkedIds != null && checkedIds.size() > 0
? checkedIds.size() : 0);j++){
if(((Integer)strList.get(i).get("id")).intValue() ==
(Integer)checkedIds.get(j).intValue()){
strList.get(i).put("checked",true);
}
}
}
}
}
java递归实例(二)---递归读取并修改Tree结构
最新推荐文章于 2021-09-20 19:10:47 发布