EXT Tree AJAX JSON的简单实现

首先定义一个通用的 Ext Tree Bean的接口,主要包括 id,name,和child属性

package com.marcoframe.core.business.businessobj;

import java.util.Set;

public interface ExtTreeJSONObject {
public String getOid();
public void setOid();
public String getName();
public void setName(String name);
public Set getChildren();
public void setChildren(Set children);
}


然后编写一个工具类,递归的方式遍历树,返回Ext Tree 所需要的json字符串

package com.marcoframe.core.business.util.json;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.List;

import net.sf.json.JSONArray;

import com.marcoframe.core.business.businessobj.ExtTreeJSONObject;

public class JSONUtil {
public static String getExtJSONTreeStr(ExtTreeJSONObject object){
JSONArray array = JSONArray.fromObject(ExtTreeJsonMap(object,null));
return array.toString();
}
private static Map ExtTreeJsonMap(ExtTreeJSONObject object,List childList){
String id = object.getOid();
String text = object.getName();
Map jsonMap = new HashMap();
jsonMap.put("id", id);
jsonMap.put("text", text);
if(childList != null)
childList.add(jsonMap);
if(object.getChildren() == null || object.getChildren().size() == 0)
jsonMap.put("leaf", "true");
else{
List children = new ArrayList();
jsonMap.put("children", children);
for(Iterator it = object.getChildren().iterator();it.hasNext();){
ExtTreeJSONObject child = (ExtTreeJSONObject) it.next();
ExtTreeJsonMap(child,children);
}
}
return jsonMap;
}
}



运行结果如下:

[quote][{"id":"root","text":"root","children":[{"id":"dp1","text":"child1","children":[{"id":"dp11","text":"child11","leaf":"true"}]},{"id":"dp2","text":"child2","leaf":"true"}]}][/quote]

如果在项目中需要用到 像 “分类”等,需要以树的形式展示,就可以实现ExtTreeJSONObject这个接口,然后调用 getExtJSONTreeStr 方法,就可以得到Ext Tree 所需要的Json 串了,可以运用 AJAX 来实现树的动态加裁
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值