簡體   English   中英

如何使用C#將JSON反序列化為派生類

[英]How to deserialize JSON to a derived class using c#

我正在嘗試映射json響應。

我有下面的JSON響應代碼

string JSON = await SendGraphRequest("/users/", $"$filter=signInNames/any(x:x/value eq '{username}')", null, HttpMethod.Get);

這是json響應

{
  "extension_7182f7a071344106a9e47cc960ab93e8_DOB": null,
  "extension_7182f7a071344106a9e47cc960ab93e8_middleName": null,
  "objectID": "",
  "accountEnabled": true,
  "email": Test
  }

我想使用以下代碼反序列化json響應

var graphUserRespModel =  JsonConvert.DeserializeObject<ResponseModelPrime>(JSON);

我正在為DeserializeObject使用三個類,但是我在所有字段上都得到了null值。 請讓我知道我做錯了什么。

 public class ResponseModelPrime
    {

        [JsonProperty(PropertyName = "odata.metadata")]
        public string OdataMetadata { get; set; }       

        [JsonProperty(PropertyName = "Status")]
        public StatusModel Status { get; set; }

        [JsonProperty(PropertyName = "objectId")]
        public string ObjectId { get; set; }

        [JsonProperty(PropertyName = "email")]
        public string Email { get; set; }

        [JsonProperty(PropertyName = "accountEnabled")]
        public bool AccountEnabled { get; set; }

        [JsonProperty(PropertyName = "DOB")]
        public string DOB { get; set; }

        [JsonProperty(PropertyName = "middleName")]
        public string middleName { get; set; }

}

 public class ResponseModel
    {        

        [JsonProperty(PropertyName = "objectId")]
        public string ObjectId { get; set; }

        [JsonProperty(PropertyName = "email")]
        public string Email { get; set; }

        [JsonProperty(PropertyName = "accountEnabled")]
        public bool AccountEnabled { get; set; }
} 

public  class ResponseModelSIT : ResponseModel
    {
        [JsonProperty(PropertyName = "extension_7182f7a071344106a9e47cc960ab93e8_DOB")]
        public string extension_7182f7a071344106a9e47cc960ab93e8_DOB { get; set; }

        [JsonProperty(PropertyName = "extension_7182f7a071344106a9e47cc960ab93e8_middleName")]
        public string extension_7182f7a071344106a9e47cc960ab93e8_middleName { get; set; }

        }

要反序列化json,您需要采用一種簡單的方式...

public Form1()
        {
            InitializeComponent();

            try
            {

            var json = @"{
                          'extension_7182f7a071344106a9e47cc960ab93e8_DOB': '17/12/1995',
                          'extension_7182f7a071344106a9e47cc960ab93e8_middleName': 'Roger',
                          'objectID': '',
                          'accountEnabled': true,
                          'email': 'Test'
                         }";

                var items = JsonConvert.DeserializeObject<ResponseModelPrime>(json);

            }
            catch (Exception ex)
            {
                var exception = ex;
            }
        }
        public class ResponseModelPrime
        {           
            [JsonProperty(PropertyName = "odata.metadata")]
            public string OdataMetadata { get; set; }

            [JsonProperty(PropertyName = "objectId")]
            public string ObjectId { get; set; }

            [JsonProperty(PropertyName = "email")]
            public string Email { get; set; }

            [JsonProperty(PropertyName = "accountEnabled")]
            public bool AccountEnabled { get; set; }

            [JsonProperty(PropertyName = "DOB")]
            public string DOB { get; set; }

            [JsonProperty(PropertyName = "middleName")]
            public string middleName { get; set; }     

            [JsonProperty(PropertyName = "extension_7182f7a071344106a9e47cc960ab93e8_DOB")]
            public string extension_7182f7a071344106a9e47cc960ab93e8_DOB { get; set; }

            [JsonProperty(PropertyName = "extension_7182f7a071344106a9e47cc960ab93e8_middleName")]
            public string extension_7182f7a071344106a9e47cc960ab93e8_middleName { get; set; }        
        }

產量

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM