Modifying MS Graph responses for display on site

做~自己de王妃 提交于 2019-12-24 12:16:41

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!