private void putModule(Module module, List<Module> list)
{
Integer moduleId = modulegetModuleId();
for (Module m : list)
{
if (mgetModuleParentId()equals(moduleId)){
if (modulegetmodules() == null ){
List<Module> modules = new ArrayList<Module>():
modulesetModules(modules);
}
modulegetModules()add(m);
}
}
}
private void parseModule(List<Module> modules, List<Module> sources)
{
for (Module m : modules)
{
putModule(m, sources);
if (mgetModules()size() > 0)
{
parseModule(mgetModules(), sources);
}
}
}
// 调用这个方法即可,插入查询回来的列表
public Module dealTopLevel(List<Module> modules){
Module module = null;
for (Module m : modules)
{
if (mgetModuleLevel() == 0){
module = m;
break;
}
}
putModule(module, modules);
if (modulegetModules()size() > 0)
{
parseModule(modulegetModules(), modules);
}
return module;
}
注意在实体类中加入 List<Module> modules;的属性
Spring直接返回这个Module即可得到结构
辛苦写的,请采纳
首先要遍历json,JSONObject的keySet类似于map可以遍历下面所有的key,
可以看到对象有好几层,并且有可能包含数组及不规则对象,如果我们想寻找所有节点下的value并找出包含"wav"的字符串并替换掉,做法是什么呢
可以看出所有包含"wav"的value都被替换了
注:文中会使用hutool工具类,需要在maven中引入
//是类似这种吗
//控制层使用JSONArray jsonObject=JSONArrayfromObject();转换
Map<String,Object> map = new HashMap<String, Object>();
mapput("id","1");
mapput("text","实验外国语学校");
List<Map<String,Object>> fatherList=new ArrayList<Map<String,Object>>();
List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
for(Bean bean:list){
if("1"equals(listgetParent_level())){
Map<String,Object> map2=new HashMap<String, Object>();
map2put("id",listgetId());
map2put("text",listgetName());
listadd(map2);
}
}
mapput("children",list);
可以,就是需要遍历所有的数据。
def print_dict(k, v):if isinstance(v, dict):
print k, v
for kk in vkeys():
print_dict(kk, v[kk])
else:
print k, v
for k in dkeys():
print_dict(k, d[k])
1、准备表结构及对应的表数据
a、表结构:
create table TB_TREE
(
CID NUMBER not null,
CNAME VARCHAR2(50),
PID NUMBER //父节点
)
b、表数据:
insert into tb_tree (CID, CNAME, PID) values (1, '中国', 0);
insert into tb_tree (CID, CNAME, PID) values (2, '北京市', 1);
insert into tb_tree (CID, CNAME, PID) values (3, '广东省', 1);
insert into tb_tree (CID, CNAME, PID) values (4, '上海市', 1);
insert into tb_tree (CID, CNAME, PID) values (5, '广州市', 3);
insert into tb_tree (CID, CNAME, PID) values (6, '深圳市', 3);
insert into tb_tree (CID, CNAME, PID) values (7, '海珠区', 5);
insert into tb_tree (CID, CNAME, PID) values (8, '天河区', 5);
insert into tb_tree (CID, CNAME, PID) values (9, '福田区', 6);
insert into tb_tree (CID, CNAME, PID) values (10, '南山区', 6);
insert into tb_tree (CID, CNAME, PID) values (11, '密云县', 2);
insert into tb_tree (CID, CNAME, PID) values (12, '浦东', 4);
2、TreeNode对象,对应tb_tree
public class TreeNode implements Serializable {
private Integer cid;
private String cname;
private Integer pid;
private List nodes = new ArrayList();
public TreeNode() {
}
//getter、setter省略
}
3、测试数据
public class TreeNodeTest {
@Test
public void loadTree() throws Exception{
Systemoutprintln(JsonUtilsjavaToJson(recursiveTree(1)));
}
/
递归算法解析成树形结构
@param cid
@return
@author jiqinlin
/
public TreeNode recursiveTree(int cid) {
//根据cid获取节点对象(SELECT FROM tb_tree t WHERE tcid=)
TreeNode node = personServicegetreeNode(cid);
//查询cid下的所有子节点(SELECT FROM tb_tree t WHERE tpid=)
List childTreeNodes = personServicequeryTreeNode(cid);
//遍历子节点
for(TreeNode child : childTreeNodes){
TreeNode n = recursiveTree(childgetCid()); //递归
nodegetNodes()add(n);
}
return node;
}
}
输出的json格式如下:
{
"cid": 1,
"nodes": [
{
"cid": 2,
"nodes": [
{
"cid": 11,
"nodes": [
],
"cname": "密云县",
"pid": 2
}
],
"cname": "北京市",
"pid": 1
},
{
"cid": 3,
"nodes": [
{
"cid": 5,
"nodes": [
{
"cid": 7,
"nodes": [
],
"cname": "海珠区",
"pid": 5
},
{
"cid": 8,
"nodes": [
],
"cname": "天河区",
"pid": 5
}
],
"cname": "广州市",
"pid": 3
},
{
"cid": 6,
"nodes": [
{
"cid": 9,
"nodes": [
],
"cname": "福田区",
"pid": 6
},
{
"cid": 10,
"nodes": [
],
"cname": "南山区",
"pid": 6
}
],
"cname": "深圳市",
"pid": 3
}
],
"cname": "广东省",
"pid": 1
},
{
"cid": 4,
"nodes": [
{
"cid": 12,
"nodes": [
],
"cname": "浦东",
"pid": 4
}
],
"cname": "上海市",
"pid": 1
}
],
"cname": "中国",
"pid": 0
}
1、方法思路使用js数组自带的filter()方法;数据格式要求,父子节点通过,Id,ParentId进行关联。默认父结节id为0。
样例数据:var jsonData = [{"id":"1","pid":"0","name":"家用电器"}, {"id":"4","pid":"1","name":"大家电"}, {"id":"5","pid":"1","name":"生活电器"}, {"id":"2","pid":"0","name":"服饰"}, {"id":"3","pid":"0","name":"化妆"}, {"id":"7","pid":"4","name":"空调"}, {"id":"8","pid":"4","name":"冰箱"}, {"id":"9","pid":"4","name":"洗衣机"}, {"id":"10","pid":"4","name":"热水器"}, {"id":"11","pid":"3","name":"面部护理"}, {"id":"12","pid":"3","name":"口腔护理"}, {"id":"13","pid":"2","name":"男装"}, {"id":"14","pid":"2","name":"女装"}, {"id":"15","pid":"7","name":"海尔空调"}, {"id":"16","pid":"7","name":"美的空调"}, {"id":"19","pid":"5","name":"加湿器"}, {"id":"20","pid":"5","name":"电熨斗"}];
2、实现方法:
function creatTreeData(list){
var clonelist = JSONparse(JSONstringify(list));
var result =clonelistfilter(function(father){
var childlist = clonelistfilter(function(child){
return fatherid= childpid;
});
if(childlistlength>0){
fatherchildren=childlist;
}
return fatherpid ==0;
});
return result;
}
3、方法封装
function treeData(list,id,pid,children){
var clone = JSONparse(JSONstringify(list));
return clonefilter(function(father){
var childlist =clonefilter(function(child){
return father[id]==child[pid];
});
if(childlistlength>0){
father[children]=childlist;
}
return father[pid]==0;
});
}
以上就是关于求java递归算法,帮我把模块表里面的数据遍历,然后转成json形式传到前台,形成树形事后加分+,非常感谢全部的内容,包括:求java递归算法,帮我把模块表里面的数据遍历,然后转成json形式传到前台,形成树形事后加分+,非常感谢、递归解析json节点并替换、如何用Java拼接JSON方式遍历整个树形节点等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)