jstree中json data 的生成

jstree官网上给出的json数据格式是这样的:

  1. <span style="font-size:14px;">// Alternative format of the node (id & parent are required)
  2. {
  3. id          : "string" // required
  4. parent      : "string" // required
  5. text        : "string" // node text
  6. icon        : "string" // string for custom
  7. state       : {
  8. opened    : boolean  // is the node open
  9. checked: boolean  // is the node checked
  10. disabled  : boolean  // is the node disabled
  11. selected  : boolean  // is the node selected
  12. },
  13. li_attr     : {}  // attributes for the generated LI node
  14. a_attr      : {}  // attributes for the generated A node
  15. }</span>

如果我想要把一个文件夹及它里面的文件用jstree显示,怎么办呢?总不能自己再按上边的格式一个一个写吧。好了,这里就涉及到一个问题,怎么样把一个路径转化成json data.

举个例子:我在E盘下有一个test文件夹,我想要把test文件夹下的所有文件用jstree目录树显示。这时候jstree的数据源就应该是test文件夹的 json数据。

test文件夹下的目录结构如下图:

test文件目录它的json格式应该是这样的:

  1. [{"attributes":{"id":"0"},"parent":"0","state":{"opened":false},"text":"test1" ,"children":[{"attributes":{"id":"1"},"parent":"0","state":{"opened":true},"text":"test1.1.txt" , "type":"leaf"  },{"attributes":{"id":"2"},"parent":"0","state":{"opened":true},"text":"test1.2.txt" , "type":"leaf"  }]  },{"attributes":{"id":"3"},"parent":"0","state":{"opened":true},"text":"test2.txt" , "type":"leaf" }]

上面的json格式中:

attributes:{id: }是每个节点的id.

parent:是这个节点的父节点的id

state:{opened:false} 表示的是这个节点的状态是不打开

children[]:表示的是这个节点的子节点。

如何把一个目录的路径,转化为上面的json格式呢?我们要用无限递归遍历这个目录,把相应信息提取出来,转化为json格式的字符串。

下面给出实现的代码:用Java写的:

  1. /**
  2. * 给定任意的路径 ,无限递归获得该路径下的所有节点,并转化为json串 ,把该json串存到root.json文件中, 用作jstree的数据源
  3. * @param f parent
  4. *        路径名 父节点ID
  5. * @return str,然后将str写入文件中。
  6. * @author YanniZhang
  7. * @date 2015.4.17
  8. */
  9. package test;
  10. import java.io.File;
  11. import java.io.FileWriter;
  12. import java.io.BufferedWriter;
  13. import java.io.IOException;
  14. public class test {
  15. public static int parentId=0;
  16. public static String str="";
  17. public  static void main(String[] args) {
  18. File f =new File("E:/test1");//给定的路径是E盘下的test文件夹。这里换成你想要转换成json串的任意路径
  19. String str="[";
  20. // 从根开始 ,调用ToJson()方法,把路径转化成json串。结果是str
  21. str+=ToJson(f,0);
  22. str += "]";
  23. /**
  24. * 把json串写入文件root.json中
  25. *
  26. */
  27. try {
  28. File file = new File("E:/root.json");
  29. // if file doesn't exists, then create it
  30. if (!file.exists()) {
  31. file.createNewFile();
  32. }
  33. FileWriter fw = new FileWriter(file.getAbsoluteFile());
  34. BufferedWriter bw = new BufferedWriter(fw);
  35. bw.write(str);
  36. bw.close();
  37. } catch (IOException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41. /*ToJson()方法,实现把指定路径转化为json串。采用无限递归遍历路径下的所有节点的方法实现*/
  42. private  static String ToJson(File f,int parent) {
  43. //杳出顶层的子节点
  44. File[] files = f.listFiles();
  45. //遍历它的子节点
  46. for(int i=0; i<files.length; i++)
  47. {
  48. //有子节点
  49. if(files[i].isDirectory())
  50. {
  51. str+= "{\"attributes\":{\"id\":\""  +parentId
  52. + "\"},\"parent\":\""  + parent
  53. + "\",\"state\":{\"opened\":false},\"text\":\"" + files[i].getName() + "\" ,";
  54. str += "\"children\":[";
  55. parent=parentId;
  56. parentId++;
  57. //遍历它的子节点
  58. File[] list=files[i].listFiles();
  59. for(int j=0;j<list.length;j++)
  60. {
  61. if(list[j].isDirectory())
  62. {
  63. //还有子节点(递归调用)
  64. str+= "{\"attributes\":{\"id\":\""  +parentId
  65. + "\"},\"parent\":\""  + parent
  66. + "\",\"state\":{\"opened\":false},\"text\":\"" + list[j].getName() + "\" ,";
  67. str += "\"children\":[";
  68. parent=parentId;
  69. parentId++;
  70. ToJson(list[j],parent);
  71. str+="]";
  72. str+="  }";
  73. if(j<list.length-1)
  74. {
  75. str+=",";
  76. }
  77. }
  78. else
  79. {
  80. str += "{\"attributes\":{\"id\":\"" + parentId
  81. + "\"},\"parent\":\""  + parent
  82. + "\",\"state\":{\"opened\":true},\"text\":\"" + list[j].getName()
  83. + "\" " + ", \"type\":\"leaf\"  }";
  84. parentId++;
  85. if (j < list.length - 1)
  86. {
  87. str += ",";
  88. }
  89. }
  90. }
  91. str+="]";
  92. str+="  }";
  93. if(i<files.length-1)
  94. {
  95. str+=",";
  96. }
  97. }
  98. else
  99. {
  100. str += "{\"attributes\":{\"id\":\"" + parentId
  101. + "\"},\"parent\":\""  + parent
  102. + "\",\"state\":{\"opened\":true},\"text\":\"" + files[i].getName()
  103. + "\" " + ", \"type\":\"leaf\" }";
  104. parentId++;
  105. if (i < files.length - 1)
  106. {
  107. str += ",";
  108. }
  109. }
  110. }
  111. return str;
  112. }
  113. }

得到的json数据存在E:/root.json

得到了任意一个路径的json 数据后。我们就要用这个json数据 生成目录树了。

如何利用上面生成的json数据请看下一篇博文:“jstree获得节点的相对路径”。

jstree中json data 的生成的更多相关文章

  1. java中json数据生成和解析(复杂对象演示)

    1.json简单介绍 1.1 json是最流行和广泛通用的数据传输格式,简称JavaScript Object Notation,最早在JavaScript中使用. 1.2 举个例子,下面是一个jso ...

  2. Swift开发中 JSON对象/JSON字符串/Data的互转

    本文将介绍Swift开发中常用的转换(JSON对象/JSON字符串/Data之间的互相转换) #pragma mark - JSON(对象)----->JSON字符串 1.原生方法 //JSON ...

  3. AJAX 中JSON 和JSONP 的区别 以及请求原理

    AJAX 跨域请求 - JSONP获取JSON数据 博客分类: Javascript /Jquery / Bootstrap / Web   Asynchronous JavaScript and X ...

  4. jQuery解析JSON出现SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data

    SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data 我在使用$.parseJSON解析后 ...

  5. jstree -- 使用JSON 数据组装成树

    概述: 前面主要是html数据,这里主要是json数组 1.格式 jsTree需要一个具体格式JSON数据,在标准的语法没有那个字段是必须的-而是那些是你需要的.请记住你可以获取任何你请求的其他属性, ...

  6. action中json的应用

    这篇文章重点介绍action中json数据的返回处理:假设须要看前端代码的一些特效或ajax的json接收,请看上一篇博客:http://blog.csdn.net/yangkai_hudong/ar ...

  7. Python中json的简单读写操作

    Python中json的简单读写操作 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的 ...

  8. android中Json的一些应用

    JSON(JavaScript Object Notation) :一种轻量级的数据交换格式,基于JavaScript的一个子集. JSON采用完全独立于语言的文本格式,使JSON成为理想的数据交换语 ...

  9. 谈谈WCF中的Data Contract(3):WCF Data Contract对Collection & Dictionary的支持

    谈谈WCF中的Data Contract(3):WCF Data Contract对Collection & Dictionary的支持 在本篇文章上一部分Order Processing的例 ...

随机推荐

  1. C#高级编程四十八天----列表

    C#中的List C#中deList怎么样?List<T>类是ArrayList类的泛型等效类,该类使用大小可按需动态增长的数组实现List<T>泛型接口. 泛型的优点:它为使 ...

  2. redis 主从 及集群

    一.redis 主从架构 搭建redis 主从   (可以用一台主机,也可以两台主机) 环境准备: 一台服务器:192.168.206.6 操作系统:CentOS7.5 redis 版本: redis ...

  3. HDMI接口基础知识及硬件设计

    参考资料:http://blog.csdn.net/u013625961/article/details/53434189: http://blog.csdn.net/u014276460/artic ...

  4. Mac OS 10.10.3下Apache + mod_wsgi配置【一】

    [一] 首先,MAC是自带Apache的,在/private/etc/apache2路径下,能够使用apachectl -v查看版本号.我的版本号例如以下: Server version: Apach ...

  5. TMS320F28335项目开发记录6_28335之cmd文件具体解释

    1.CMD文件的作用 CMD文件的作用就像仓库的货物摆放记录一样,为程序代码和数据分配指定的空间. 2.C语言生成的段 C语言生成的段大致分为两大类:初始化和未初始化,已初始化的段含有真正的指令和数据 ...

  6. salt-stack "No Top file or external nodes data matches found"解决

    salt-stack在配置分组时提示如下信息: No Top file or external nodes data matches found 后来在官网上找到如下提示,意思是需要重启master服 ...

  7. MVC 登录后重定向来最初请求的 URL

    1.在登录的“Action” 方法中接收“ReturnUrl”参数. 2.在验证登录的“Action”方法中登录成功后,判断如果“ReturnUrl”不为空就跳转到“ReturnUrl”指向的页面. ...

  8. 视频生成 量产 win 转 linux ffmpeg linux 安装 对批量视频的尽可能短时间生成

    环境准备 Welcome to aliyun Elastic Compute Service! [root@mytest ~]# pip install baidu-aip Looking in in ...

  9. data-toggle data-target

    data-toggle https://stackoverflow.com/questions/30629974/how-does-the-data-toggle-attribute-work-wha ...

  10. USACO Section 1.2PROB Miking Cows

    贪心做过去,先对每个时间的左边点进行排序,然后乱搞,当然线段树也可以做 /* ID: jusonal1 PROG: milk2 LANG: C++ */ #include <iostream&g ...