如何使用C#将JSON反序列化为派生类

时间:2019-05-28 10:17:28

标签: c# json odata

我正在尝试映射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; }

        }

1 个答案:

答案 0 :(得分:1)

要反序列化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; }        
        }

Output