2017-07-07 33 views
1

我正在使用C#LdapConnection來獲取SearchResponse的工作。我可以將SearchResponse轉換爲Json字符串,但無法將該Json字符串放入具有模仿SearchResponse的屬性的對象類中。 Json字符串格式正確。我是否以錯誤的方式使用Newtonsoft.Json庫?有一個更好的方法嗎?我唯一的要求是我得到一個SearchResponse到我的對象類(DirectoryEntity),所以我可以從那裏使用它。LDAP SearchResponse到Json C#

我的控制檯應用程序:

class Program 
    { 
     static void Main(string[] args) 
     { 
      LdapClient client = new LdapClient(); 
      List<LdapSearchResponseModel> list = new List<LdapSearchResponseModel>(); 
      string query = "(|(uupid=name1)(uupid=name2))"; 
      string[] attributes = new string[] { }; 
      string host = "id.directory.univ"; 
      int port = 1234; 
      SearchResponse response = client.RawQuery(query, attributes, host, port); 

      string json = JsonConvert.SerializeObject(response); 

      var test = JsonConvert.DeserializeObject<DirectoryEntities>(json); 
     } 
    } 

我DirectoryEntity類:

public class DirectoryEntity 
    { 
     public string EntityDN { get; set; } 
     public int MailStop { get; set; } 
     public List<string> UniversityAffiliation { get; set; } 
     public int UniversityId { get; set; } 
     public string GivenName { get; set; } 
     public string Title { get; set; } 
     public string PasswordState { get; set; } 
     public string MiddleName { get; set; } 
     public string AccountState { get; set; } 
     public int Uid { get; set; } 
     public string Mail { get; set; } 
     public string MailPreferredAddress { get; set; } 
     public DateTime DateOfBirth { get; set; } 
     public List<string> GroupMembership { get; set; } 
     public string DegreeType { get; set; } 
     public int ClassLevelCode { get; set; } 
     public string AuthId { get; set; } 
     public string Major { get; set; } 
     public string ClassLevel { get; set; } 
     public bool SupressDisplay { get; set; } 
     public string UnderGraduateLevel { get; set; } 
     public string ObjectClass { get; set; } 
     public int DepartmentNumber { get; set; } 
     public List<string> EduPersonAffiliation { get; set; } 
     public string LocalPostalAddress { get; set; } 
     public string Uupid { get; set; } 
     public string LocalPhone { get; set; } 
     public string TelephoneNumber { get; set; } 
     public string Department { get; set; } 
     public string Sn { get; set; } 
    } 

我DirectoryEntities類:

public class DirectoryEntities 
{ 
    public List<DirectoryEntity> Entities { get; set; } 
    public int Count { get; set; } 
} 
+0

您是否嘗試過使用[的DirectorySearcher(https://msdn.microsoft.com/en-us/library/system.directoryservices.searchresult(V = vs.110)的.aspx)?它返回一個DirectoryEntry類。 –

回答

0

您是否嘗試過明確定義爲DirectoryEntity類的默認構造函數?我相信Newtonsoft要求對象在可以反序列化之前有一個默認的構造函數。嘗試添加該到DirectoryEntity類:

public DirectoryEntity() { }