0

I am trying to map the json response.

I have below json reponse code

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

this the json response

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

I want to deSerialize the json response, by using below code

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

I am using the three classes for DeserializeObject But i am getting the null value on all the fields. please let me know what mistake my doing.

 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; } } 
7
  • 1
    Add json in question which you deserialize. Commented May 28, 2019 at 10:19
  • 1
    You've shown the code that gets the json, not the json itself.. Commented May 28, 2019 at 10:20
  • Why don't you use OData's client library? You're trying to deserialize something that contains the schema as well as the object to a class that can't handle this. Even if you modify your class to get the code to work, the metadata will still be lost Commented May 28, 2019 at 10:30
  • In fact, the advantages of OData over raw JSON are that you can generate client classes easily based on that metadata. You can also use LINQ to retrieve only the fields you want. Commented May 28, 2019 at 10:34
  • @PanagiotisKanavos so what i have to do Commented May 28, 2019 at 10:42

1 Answer 1

1

For deserializing json, you need to go in a simple way...

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.