Goal : To convert json string of graph api to respective object
When we call to microsoft graph api, sometimes we get response like following:
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users",
"value": [
{
"businessPhones": [],
"displayName": "Conf Room Adams",
"givenName": null,
"jobTitle": null,
"mail": "Adams@M365x214355.onmicrosoft.com",
"mobilePhone": null,
"officeLocation": null,
"preferredLanguage": null,
"surname": null,
"userPrincipalName": "Adams@M365x214355.onmicrosoft.com",
"id": "6e7b768e-07e2-4810-8459-485f84f8f204"
},
{
"businessPhones": [
"+1 425 555 0109"
],
"displayName": "Adele Vance",
"givenName": "Adele",
"jobTitle": "Product Marketing Manager",
"mail": "AdeleV@M365x214355.onmicrosoft.com",
"mobilePhone": null,
"officeLocation": "18/2111",
"preferredLanguage": "en-US",
"surname": "Vance",
"userPrincipalName": "AdeleV@M365x214355.onmicrosoft.com",
"id": "87d349ed-44d7-43e1-9a83-5f2406dee5bd"
}
}
To get above response we are using
var content = await response.Content.ReadAsStringAsync();
Now the question is how we can convert this json formatted string to specific object or list of object?
You can use following lines:
var userList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<EmailAddress>>(Newtonsoft.Json.Linq.JObject.Parse(content).GetValue("value").ToString());
In above statements, first we are converting that json string to Jobject and getting value property only. Now what I need to do is just deserialize the json to respective object(ex. List). I took here List as I have json string for that but you can convert as per for which response you get json. If you request for meeting rooms than you will get json response for that and you deserialize with List.