2017-02-19 19 views
1

我正在查詢AAD上的Graph API以獲取組成員信息。由此,我得到一個包含用戶所屬的一個或多個組的JSON字符串。我知道我可以對一個結構進行逆向工程並對其進行反序列化,但是可以使用Graph API結構作爲我可以利用的某種契約嗎?從C#中的Graph API查詢中提取數據

我發現了一些針對獲取原始字符串數據的Graph API查詢的例子,但我真的可以使用一個簡單的例子,其中有人提取原始信息(例如組ID或組名)並將其用於結構體。

+0

是否有可能舉例說明您收到的JSON以及您希望獲得的結構?我認爲一個可靠的例子將有助於澄清你在找什麼。 :) –

回答

0

創建一個用戶類,如下

public class ActiveDirectoryUser 
    { 
     public bool accountEnabled { get; set; } 
     public List<SignInName> signInNames { get; set; } 
     public string creationType { get; set; } 
     public string displayName { get; set; } 
     public string mailNickname { get; set; } 

     [JsonProperty("passwordProfile", NullValueHandling = NullValueHandling.Ignore)] 
     public PasswordProfile PasswordProfile { get; set; } 

     [JsonProperty("passwordPolicies", NullValueHandling = NullValueHandling.Ignore)] 
     public string PasswordPolicies { get; set; } 

     public string city { get; set; } 
     public object country { get; set; } 
     public object facsimileTelephoneNumber { get; set; } 
     public string givenName { get; set; } 
     public object mail { get; set; } 
     public object mobile { get; set; } 
     //public List<object> otherMails { get; set; } 
     public string postalCode { get; set; } 
     public object preferredLanguage { get; set; } 
     public string state { get; set; } 
     public object streetAddress { get; set; } 
     public string surname { get; set; } 
     public Guid objectId { get; set; } 
     public object telephoneNumber { get; set; } 


    } 

    public class SignInName 
    { 
     public string type { get; set; } 
     public string value { get; set; } 
    } 

    public class PasswordProfile 
    { 
     public string password { get; set; } 
     public bool forceChangePasswordNextLogin { get; set; } 
    } 

當正從AD的反應,使用newtonsoft.json序列化到如下面相應的類。

var response = await httpClient.SendAsync(request); 

if (response.IsSuccessStatusCode) 
{ 
    var result = response.Content.ReadAsStringAsync().Result;           
    return JsonConvert.DeserializeObject<ActiveDirectoryUser>(result); 
} 

訣竅是JsonConvert.DeserializeObject <>(結果);