2017-07-07 17 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() { }