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結構
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.