1

Active Directory OU から有効なjqTree JSON 構造を作成する必要があります。これを再帰メソッド(InfoNode)でテストしているのですが、取得できません。

結果の json は文字列 json に入ります。処理する最初のノードは、デフォルトのドメイン ルートを持つ DirectoryEntry 親です。再帰メソッド (InfoNode) は、現在の子ノードを取得し、「OU」でフィルター処理して、JSON プロパティの「label」と「path」を作成します。このノードが現在の JSON アイテムの最後を書き込むための子をさらに持っているかどうかをチェックする前に。最後に、さらに子がある場合は、メソッド (InfoNode) を再度実行します。

public static DirectoryEntry root = new DirectoryEntry("LDAP://RootDSE");
public string dom = (string)root.Properties["defaultNamingContext"].Value;

public string json = "";

public Form1()
{
    InitializeComponent();
    DirectoryEntry parent = new DirectoryEntry("LDAP://" + dom);
    InfoNode(parent);
    System.IO.File.WriteAllText(@"json.txt", json);
}

void InfoNode(DirectoryEntry node)
{
    string[] arr = node.Name.Split('=');

    if (arr[0] == "OU")
    {
        json += "'children':[{'label':'" + arr[1] + "','path':'" + node.Path + "'";

        if (nodo.Children == null)
        {
            json += "}]";
        }
        else
        {
            json += ",";
        }              
    }

    if (node.Children != null)
    {
        foreach (DirectoryEntry child in node.Children)
        {
            InfoNode(child);
        }
    }
}
4

1 に答える 1

0

コードで何が失敗したかについて、より詳細を提供する必要があります。

やってみます:-)

以下のようにコードを変更してみてください。最適ではありません (分割する前に startswith を使用し、より多くの string.Format を使用し、メソッドを再帰的に呼び出す前に startswith で子をテストする方がよいでしょう)、それはうまくいくはずです。

ツリーを進めていくと、ldap ソースから子をロードする必要があるかもしれません。

public string json = "";

public Form1()
{
    InitializeComponent();
    DirectoryEntry parent = new DirectoryEntry("LDAP://" + dom);
    json = InfoNode(parent);
    System.IO.File.WriteAllText(@"json.txt", json);
}

public string InfoNode(DirectoryEntry node)
{
    string[] arr = node.Name.Split('=');
    var result = string.Empty;

    if (arr[0].Equals("ou",StringComparison.InvariantCultureIgnoreCase))
    {
        result = "'{'label':'" + arr[1] + "','path':'" + node.Path + "'";

        if (node.Children.Cast<DirectoryEntry>().Any())
        {
            result += String.Format(", children:[{0}]",
                                    String.Join(",\n ",
                                                node.Children.Cast<DirectoryEntry>()
                                                    .Select(InfoNode).Where(s=>!string.IsNullOrEmpty(s))
                                                    .ToArray()));
        }
        result += "}";
    }
    return result;
}
于 2012-12-04T13:12:31.113 に答える