• jstree中json data 的生成


                                                   jstree中json data 的生成

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

    [html] view plain copy
     
    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格式应该是这样的:

    [plain] view plain copy
     
    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写的:

    [java] view plain copy
     
    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.   
    11. import java.io.File;  
    12. import java.io.FileWriter;  
    13. import java.io.BufferedWriter;  
    14. import java.io.IOException;  
    15.   
    16. public class test {  
    17.     public static int parentId=0;  
    18.     public static String str="";  
    19.     public  static void main(String[] args) {  
    20.           
    21.          File f =new File("E:/test1");//给定的路径是E盘下的test文件夹。这里换成你想要转换成json串的任意路径  
    22.          String str="[";  
    23.             // 从根开始 ,调用ToJson()方法,把路径转化成json串。结果是str   
    24.             str+=ToJson(f,0);    
    25.             str += "]";    
    26.            /**  
    27.             * 把json串写入文件root.json中 
    28.             *  
    29.             */  
    30.             try {  
    31.                    
    32.                      
    33.                    File file = new File("E:/root.json");  
    34.                    
    35.                    // if file doesn't exists, then create it  
    36.                    if (!file.exists()) {  
    37.                     file.createNewFile();  
    38.                    }  
    39.                    
    40.                    FileWriter fw = new FileWriter(file.getAbsoluteFile());  
    41.                    BufferedWriter bw = new BufferedWriter(fw);  
    42.                    bw.write(str);  
    43.                    bw.close();  
    44.                                                
    45.                   } catch (IOException e) {  
    46.                    e.printStackTrace();  
    47.                   }  
    48.           
    49.     }  
    50.     
    51.       
    52.     /*ToJson()方法,实现把指定路径转化为json串。采用无限递归遍历路径下的所有节点的方法实现*/  
    53.      private  static String ToJson(File f,int parent) {  
    54.            
    55.           //杳出顶层的子节点  
    56.            
    57.           File[] files = f.listFiles();  
    58.             
    59.       
    60.           //遍历它的子节点  
    61.           for(int i=0; i<files.length; i++)   
    62.           {          
    63.            //有子节点  
    64.            if(files[i].isDirectory())  
    65.            {  
    66.                  
    67.                 
    68.                str+= "{"attributes":{"id":""  +parentId   
    69.                        + ""},"parent":""  + parent  
    70.                        + "","state":{"opened":false},"text":"" + files[i].getName() + "" ,";    
    71.                str += ""children":[";   
    72.                parent=parentId;  
    73.                parentId++;  
    74.                 
    75.                  
    76.                  
    77.                //遍历它的子节点  
    78.                File[] list=files[i].listFiles();  
    79.                for(int j=0;j<list.length;j++)  
    80.                {  
    81.                   
    82.                    
    83.                    if(list[j].isDirectory())  
    84.                    {  
    85.                          
    86.                        //还有子节点(递归调用)  
    87.                        str+= "{"attributes":{"id":""  +parentId   
    88.                                + ""},"parent":""  + parent  
    89.                        + "","state":{"opened":false},"text":"" + list[j].getName() + "" ,";    
    90.                        str += ""children":[";   
    91.                        parent=parentId;  
    92.                        parentId++;  
    93.                         
    94.                        ToJson(list[j],parent);  
    95.                        str+="]";  
    96.                        str+="  }";  
    97.                        if(j<list.length-1)  
    98.                        {  
    99.                            str+=",";  
    100.                        }  
    101.                         
    102.                        
    103.                    }  
    104.                  
    105.                    else  
    106.                    {  
    107.                      str += "{"attributes":{"id":"" + parentId    
    108.                            + ""},"parent":""  + parent  
    109.                        + "","state":{"opened":true},"text":"" + list[j].getName()    
    110.                            + "" " + ", "type":"leaf"  }";    
    111.                       
    112.                      parentId++;  
    113.                       
    114.                      if (j < list.length - 1)    
    115.                      {    
    116.                        str += ",";    
    117.                      }  
    118.                      
    119.                        
    120.                        
    121.                    }    
    122.                      
    123.               }  
    124.                str+="]";  
    125.                str+="  }";  
    126.                if(i<files.length-1)  
    127.                {  
    128.                    str+=",";  
    129.                }  
    130.            }  
    131.              
    132.                else  
    133.                {  
    134.                    str += "{"attributes":{"id":"" + parentId    
    135.                            + ""},"parent":""  + parent  
    136.                        + "","state":{"opened":true},"text":"" + files[i].getName()    
    137.                            + "" " + ", "type":"leaf" }";    
    138.                     parentId++;  
    139.                    if (i < files.length - 1)    
    140.                    {    
    141.                        str += ",";    
    142.                    }    
    143.                }    
    144.             
    145.      }  
    146.      return str;  
    147. }  
    148. }  
    149.                
    150.              
    151.                
    152.               
    153.            
    154.   
    155.   
    156.     
    157.       
    158.       

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

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

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

  • 相关阅读:
    常用php操作redis命令整理(四)SET类型
    常用php操作redis命令整理(三)LIST类型
    常用php操作redis命令整理(二)哈希类型
    常用php操作redis命令整理(一)通用及字符串类型
    php时间戳函数mktime()
    Linux SSH远程文件/目录传输命令scp
    运行一个android程序,直接访问某个网站
    Android开发学习笔记:浅谈WebView
    Android 权限添加
    linux下使用vi操作
  • 原文地址:https://www.cnblogs.com/telwanggs/p/7389562.html
Copyright © 2020-2023  润新知