一直知道递归是调用自己的方法,就是不知道怎么写,帮我把这个json用递归遍历出来源码,学习学习,谢谢

解决方案 »

  1.   


    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <link href="http://wwwendt.de/tech/dynatree/src/skin/ui.dynatree.css" rel="stylesheet" type="text/css">
    <script src="http://wwwendt.de/tech/dynatree/src/jquery.dynatree.js" type="text/javascript"></script>

    <div id="tree"> </div> <script>
        var data =
            [
                {"id": 12, "text": "节点1", "children":
                        [
                            { "id": 11, "text": "节点1.1" }
                        ]
                },
                {"id": 12, "text": "节点1", "children":
                        [
                            {"id": 11, "text": "节点1.1", "children":
                                    [
                                        { "id": 11, "text": "节点1.1" }
                                    ]
                            }
                        ]
                }
            ];     function processNodes(nodes) {
         for (var i=0; i<nodes.length; i++) {
         nodes[i].title = nodes[i].text;
         if (nodes[i].children)
         processNodes(nodes[i].children);
         }
    return nodes;
        }
        
        $(function () {
         $("#tree").dynatree({
         children: processNodes(data)
         });
        });
    </script>
      

  2.   

    謝謝你但是我不是需要這樣的,我是要在cs页用代码把这json拼接起来的
      

  3.   

    [
        { "id":12,"text": "节点1", "children":
        [
               { "id":11,"text": "节点1.1"     }
            ]
        },
    { "id":12,"text": "节点1", "children":
        [
               { "id":11,"text": "节点1.1" ,"children":
          [
         { "id":11,"text": "节点1.1" }
      ]
       }
            ]
        }
    ]id跟text的值都是从数据库里查询出来,在用程序把json拼接,就可以显示出下拉树形
    ds.Tables[0].Rows[i]["parent"]这是id值
    ds.Tables[0].Rows[i]["text"]    text的值
      

  4.   


    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;public class Program
    {
    static void Main()
    {
    string conn = "server=xxx.xxx.xxx.xxx;database=xxxx;uid=xxx;pwd=xxx";
    string cmd = "select * from xxxxx";
    var da = new SqlDataAdapter(cmd, conn);
    var dt = new DataTable();
    da.Fill(dt); Func<DataRow, Node> row2node = null;
    row2node = row => new Node
    {
    id = row.Field<string>("MENUID"),
    text = row.Field<string>("CAPTION"),
    children = dt.Select("[PARENT]='" + row.Field<string>("MENUID") + "'").Select(r => row2node(r)).ToList()
    }; var nodes = dt.Select("[PARENT]='0'").Select(r => row2node(r));
    string json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(nodes); // 引用 System.Web.Extensions
    Console.WriteLine(json);
    } private class Node
    {
    public string id { get; set; }
    public string text { get; set; }
    public List<Node> children { get; set; }
    }
    }
      

  5.   

    不好意思是我弄错了,能帮我解释下这样写什么意思吗,谢谢row2node = row => new Node
    {
    id = row.Field<string>("MENUID"),
    text = row.Field<string>("CAPTION"),
    children = dt.Select("[PARENT]='" + row.Field<string>("MENUID") + "'").Select(r => row2node(r)).ToList()
    };
      

  6.   

    这段等价于: private static Node row2node(DataRow row, DataTable dt)
    {
    return new Node
    {
    id = row.Field<string>("MENUID"),
    text = row.Field<string>("CAPTION"),
    children = dt.Select("[PARENT]='" + row.Field<string>("MENUID") + "'").Select(r => row2node(r, dt)).ToList()
    };
    }这就是个自己调用自己的递归方法。