六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 43|回复: 0

EXT Tree AJAX JSON的简单实现

[复制链接]

升级  20%

2

主题

2

主题

2

主题

童生

Rank: 1

积分
10
 楼主| 发表于 2013-1-23 01:46:41 | 显示全部楼层 |阅读模式
首先定义一个通用的 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;}}


运行结果如下:

<div class="quote_title">引用
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表