Как десериализовать JSON в производный класс, используя c # - PullRequest
0 голосов
/ 28 мая 2019

Я пытаюсь отобразить ответ 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, но я получаю нулевое значение во всех полях. пожалуйста, дайте мне знать, какую ошибку я совершаю.

 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 Ответ

1 голос
/ 28 мая 2019

Для десериализации 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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...