问题
I have a c# Web App that connects to Azure AD B2C directory with MS Graph to display user data. I'm looking for a best practice way to display the contents returned by Graph Query. I could always do straight forward string formatting, but I'm looking for something more optimal.
I use
HttpResponseMessage httpmsg = await QueryGraphAsync("/users?$select=displayName");
and the response looks like this :
{"@odata.context":"https://graph.microsoft.com/beta/$metadata#users(displayName)","value":[{"displayName":"xxx@xxx.xxx Xxx"}]}
There is just one user at this momment. What I'd like to get is "xxx@xxx.xxx Xxx" without playing around with string modification that would resolve around finding displayName, skipping a few chars and reading up untill it finds a quotation mark.
回答1:
The Microsoft Graph is a REST API which returns JSON. Rather than doing "string modification" you should treat the response as a JSON object, which can easily be converted into an array or dictionary.
var obj = JSON.parse('{"@odata.context":"https://graph.microsoft.com/beta/$metadata#users(displayName)","value":[{"displayName":"xxx@xxx.xxx Xxx"}]}')
var str = JSON.stringify(obj.value[0].displayName)
document.write(str)
This is how you should treat all of the responses you get from the Microsoft Graph. Let me know if this helps!
来源:https://stackoverflow.com/questions/46243449/modifying-ms-graph-responses-for-display-on-site