public class TreeNode {

private long nodeId;

    private String nodeName;

    private long fatherNodeId;



    public TreeNode() {

    }

    public TreeNode(long nodeId, String nodeName, long fatherNodeId) {

        this.nodeId = nodeId;

        this.nodeName = nodeName;

        this.fatherNodeId = fatherNodeId;

    }

    public long getNodeId() {

        return nodeId;

    }

    public void setNodeId(long nodeId) {

        this.nodeId = nodeId;

    }

    public String getNodeName() {

        return nodeName;

    }

    public void setNodeName(String nodeName) {

        this.nodeName = nodeName;

    }

    public long getFatherNodeId() {

        return fatherNodeId;

    }

    public void setFatherNodeId(long fatherNodeId) {

        this.fatherNodeId = fatherNodeId;

    }

}

public class ConstructorTree {

    StringBuffer json = new StringBuffer();

    /**

     * 

     * @Title: getJson

     * @Description: 调用接口

     * @author Administrator

     * @param list

     * @param node

     * @return

     */

    public String getJson(List<TreeNode> list, TreeNode node) {

        json = new StringBuffer();

        constructorJson(list, node);

        String jsonDate = json.toString();

        return ("[" + jsonDate + "]").replaceAll(",]", "]");





    }



    /**

     * 

     * @Title: constructorJson

     * @Description: 构建json树

     * @author Administrator

     * @param list

     * @param treeNode

     */

    @SuppressWarnings("unchecked")

    public void constructorJson(List<TreeNode> list, TreeNode treeNode) {

        if (hasChild(list, treeNode)) {

            json.append("{id:");

            json.append(treeNode.getNodeId());

            json.append(",text:");

            json.append(treeNode.getNodeName());

            json.append(",children:[");

            List<TreeNode> childList = getChildList(list, treeNode);

            Iterator iterator = childList.iterator();

            while (iterator.hasNext()) {

                TreeNode node = (TreeNode) iterator.next();

                constructorJson(list, node);

            }

            json.append("]},");

        } else {

            json.append("{id:");

            json.append(treeNode.getNodeId());

            json.append(",text:");

            json.append(treeNode.getNodeName());

            json.append("},");

        }

    }



    /**

     * 

     * @Title: getChildList

     * @Description: 获得子节点列表信息

     * @author Administrator

     * @param list

     * @param node

     * @return

     */

    public List<TreeNode> getChildList(List<TreeNode> list, TreeNode node) {

        List<TreeNode> li = new ArrayList<TreeNode>();

        Iterator it = list.iterator();

        while (it.hasNext()) {

            TreeNode n = (TreeNode) it.next();

            if (n.getFatherNodeId() == node.getNodeId()) {

                li.add(n);

            }

        }

        return li;

    }



    /**

     * 

     * @Title: hasChild

     * @Description: 推断是否有子节点

     * @author Administrator

     * @param list

     * @param node

     * @return

     */

    public boolean hasChild(List<TreeNode> list, TreeNode node) {

        return getChildList(list, node).size() > 0 ? true : false;

    }

}

获得树形json串的更多相关文章

  1. iOS 字典或者数组和JSON串的转换

    在和服务器交互过程中,会iOS 字典或者数组和JSON串的转换,具体互换如下: // 将字典或者数组转化为JSON串 + (NSData *)toJSONData:(id)theData { NSEr ...

  2. [原创] C# dynamic拼接Json串

    using Newtonsoft.Json; 之前拼接两个json串,是用的这样的代码 , json1.Length - ); json2 = json2.Insert(json2 - , tmp); ...

  3. spring入门(七)【springMVC返回json串】

    现在多数的应用为了提高交互性多使用异步刷新,即在不刷新整个页面的情况下,只刷新局部,局部刷新用得最多就是ajax,ajax和后台进行交互的数据格式使用的最多的是JSON,这里简单描述,在springm ...

  4. java对象与json串互转

    1:java对象与json串转换: java对象—json串: JSONObject JSONStr = JSONObject.fromObject(object); String str = JSO ...

  5. spring 4.x下让http请求返回json串

    当前很多应用已经开始将响应返回为json串,所以基于springframework框架开发的服务端程序,让响应返回json字符串成为了一种常用手段. 这里介绍一下如何在spring-MVC框架下方便快 ...

  6. curl运行json串,代理转发格式

    curl -b 'uin=o0450654733; skey=@tq9xjRvYy' -H "Content-Type: application/json" -X POST -d ...

  7. 使用FastJSON,将对象或数组和JSON串互转

    Fastjson,是阿里巴巴提供的一个Java语言编写的高性能功能完善的JSON库.其开源的下载网址为:https://github.com/AlibabaTech/fastjson. 示例代码如下: ...

  8. 利用QJSON将FDQuery转成JSON串

    服务器要支持Http协议,打算采用Http+JSON的方式来交换数据.一开始考虑使用superobject,因为以前使用比较多,比较熟悉. 代码如下: class function FDQueryTo ...

  9. 怎么解析json串在.net中

    以前知道一种解析json串的方法,觉得有点麻烦.就从别的地方搜到了另一种 string json = vlt.getlist(); JObject jo = JObject.Parse(json); ...

随机推荐

  1. IOS 应用的架构解析

    首先新建一个IOS 的应用工程,主要讲解一下的文件组成: main.m XXXXDelegete.h\.m MainWindow.xib info.plist 文件 IOS 应用程序由UIKit 封装 ...

  2. excel使用cube

  3. Locally weighted linear regression(局部加权线性回归)

    (整理自AndrewNG的课件,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) 前面几篇博客主要介绍了线性回归的学习算法,那么它有什么不足的地方么 ...

  4. android:改动PagerTabStrip中的背景颜色,标题字体的样式、颜色和图标以及指示条的颜色

    1.改动PagerTabStrip中的背景颜色 我们在布局中直接设置background属性就可以: <android.support.v4.view.ViewPager android:id= ...

  5. C语言 realloc为什么要有返回值,realloc返回值具体解释/(解决随意长度字符串输入问题)。

    在C语言操作中会用到大量的内存操作,当中非经常常使用的一个是realloc(). 由字面意思能够知道,该函数的作用是用于又一次分配内存. 使用方式例如以下: NewPtr=(数据类型*)realloc ...

  6. Swift - 多行文本输入框(UITextView)的用法

    1,多行文本控件的创建 1 2 3 4 var textview=UITextView(frame:CGRectMake(10,100,200,100)) textview.layer.borderW ...

  7. 用"池"来提升对象的复用

    对象池化是目前常用的一种系统优化的技术.通俗的说也就是一个对象不用多次的被实例化,来消耗性能,可以把这些常用的类放入一个池中,当需要的时候在去池里去拿去,不用的时候 在放入池中.可以叫做对象池.他可以 ...

  8. 关于__stdcall和__cdecl调用方式的理解

    __stdcall和__cdecl都是函数调用约定关键字,先给出这两者的区别,然后举实例分析: __stdcall:参数由右向左压入堆栈:堆栈由函数本身清理. __cdecl:参数也是由右向左压入堆栈 ...

  9. [ACM] hdu 4405 Aeroplane chess (概率DP)

    Aeroplane chess Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 ...

  10. shell脚本查看网络配置

    #!/bin/bash ifconfig|grep -E 'eth|inet'|grep -Ev '(inet6|127.0.0.1)'|sed 's/ /\n/g'|awk NF|grep -Ev ...