Java实现zTree简单数据格式转换easyui Tree数据格式

package com.flyer.algorithm;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 简单数据转换easyui Tree数据格式工具类.
 * Created by xufy on 2017/12/19.
 */
public class DataTransformUtil {
    public static void main(String[] args) throws FileNotFoundException,IOException{
        File file = new File("D:/test.txt");
        InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file));
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        StringBuffer sb = new StringBuffer();
        String str = null;
        while ((str = bufferedReader.readLine()) != null ) {
            sb.append(str);
        }
        bufferedReader.close();
        inputStreamReader.close();
        List<Map<String,Object>> paramList = JSON.parseObject(sb.toString(),new TypeReference<List<Map<String,Object>>>() {});
        List<Tree> result = transformToEasyuiTree("ORG_ID","ORG_NAME","PARENT_ORG_ID","-1","open",false,paramList);
        System.out.print(JSON.toJSONString(result));
    }
    /*
    * 转换方法.
    */
    public static List<Tree> transformToEasyuiTree(String id,String text,String pid,String pidValue,
                       String state,Boolean checked,List<Map<String,Object>> paramList){
        if (0 >= paramList.size()) {
            return null;
        }
        List<Tree> rootList = new ArrayList<Tree>();
        Tree root = null;
        for (Map<String,Object> param:paramList) {
            if (pidValue.equals(param.get(pid))) {
                root = mapToTree(id,text,state,checked,param);
                rootList.add(root);
            }
        }
        if (0 < rootList.size()) {
            for (Tree tree:rootList) {
                addChildren(tree,id,text,pid,state,checked,paramList);
            }
        }

        return rootList;
    }
    /*
    * 为每个节点添加子节点.
    */
    public static Tree addChildren(Tree root,String id,String text,String pid,String state,Boolean checked,List<Map<String,Object>> paramList) {
        for (Map<String,Object> param:paramList) {
            if (root.id.equals(param.get(pid))){
                List<Tree> children = root.getChildren();
                children.add(mapToTree(id,text,state,checked,param));
            }
        }
        if (0 < root.getChildren().size()) {
            for (int i = 0; i < root.getChildren().size(); i++) {
                addChildren(root.getChildren().get(i),id,text,pid,state,checked,paramList);
            }
        }
        return root;
    }
    /*
    * map转换成tree.
    *
    */
    public static Tree mapToTree(String id,String text,String state,Boolean checked,Map<String,Object> map) {
        if (null == map) {
            return null;
        }
        Tree tree = new Tree();
        Map<String,Object> attrMap = new HashMap<String,Object>();
        for (Map.Entry<String,Object> set:map.entrySet()) {
            if (id.equals(set.getKey())) {
                tree.setId((String)set.getValue());
            } else if (text.equals(set.getKey())) {
                tree.setText((String)set.getValue());
            } else {
                attrMap.put(set.getKey(),set.getValue());
            }
        }
        if (state != null && state != "") {
            tree.setState(state);
        }
        tree.setAttributes(attrMap);
        tree.setChecked(checked);
        tree.setChildren(new ArrayList<Tree>());
        return tree;
    }
    /*
    * 内部类.
    */
    public static class Tree{
        private String id;
        private String text;
        private String state;
        private Boolean checked;
        private Map<String,Object> attributes;
        private List<Tree> children;
        public Tree(){
            state = "open";
            checked = false;
        }
        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public String getState() {
            return state;
        }

        public void setState(String state) {
            this.state = state;
        }

        public Boolean getChecked() {
            return checked;
        }

        public void setChecked(Boolean checked) {
            this.checked = checked;
        }

        public Map<String, Object> getAttributes() {
            return attributes;
        }

        public void setAttributes(Map<String, Object> attributes) {
            this.attributes = attributes;
        }

        public List<Tree> getChildren() {
            return children;
        }

        public void setChildren(List<Tree> children) {
            this.children = children;
        }
    }
}
 

转载于:https://my.oschina.net/u/3651689/blog/1599978

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值