2012-12-04 2 views
2

Active Directory OU에서 유효한 jqTree JSON 구조를 만들어야합니다. 이 재귀 메서드 (InfoNode)를 사용하여 테스트 중이지만 가져올 수 없습니다.Active Directory OU 트리 to jqTree

결과 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); 
     } 
    } 
} 

답변

0

코드에 어떤 문제가 있는지 자세히 설명해야합니다.

나는 당신이 아래와 같이 코드를 수정하려고 할 수 있습니다

:-) 시도를 줄 것이다. (splitting하기 전에 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; 
}