qq12187590 发表于 2013-1-29 11:40:01

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;}}


运行结果如下:

<div class="quote_title">引用
页: [1]
查看完整版本: EXT Tree AJAX JSON的简单实现