Telamont
Telamont

Reputation: 41

Microsoft.Graph Unable to deserialize the response

I am quite new to programming and especially Microsoft.Graph

I am having problems handling the response to: https://graph.microsoft.com/v1.0/me/drive/root/children

the response looks like this (just much longer):

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('xyz%40hotmail.com')/drive/root/children",
    "value": [
        {
            "createdBy": {
                "user": {
                    "displayName": "xyz",
                    "id": "cf58e4781082"
                }
            },
            "createdDateTime": "2009-01-08T08:52:07.063Z",
            "cTag": "adDpFREJDR4RTQMTgxMDgyITEyOC42MzYxODM0MTU0Mjc3MDAwMDA",
            "eTag": "aRURCQ0Y1OEU0A4MiExMjguMA",
            "id": "EDBCF58E471082!128",
            "lastModifiedBy": {
                "user": {
                    "displayName": "xyz",
                    "id": "edbcf58e48082"
                }
            }, ............. etc...

The response that I received is correct, in JSON format (I believe ><), but I cannot figure out how to parse it into an array containing the folders name.

Please help!

Upvotes: 4

Views: 4485

Answers (2)

Michael Mainer
Michael Mainer

Reputation: 3475

Have considered using the Microsoft Graph client library? It will deserialize the JSON. Your call will look like this:

// Return all children files and folders off the drive root.
var driveItems = await graphClient.Me.Drive
                                     .Root
                                     .Children
                                     .Request()
                                     .GetAsync();

foreach (var item in driveItems)
{
    // Get your item information 
}

Here's some samples to help you get started: https://github.com/microsoftgraph?utf8=%E2%9C%93&q=csharp

Upvotes: 1

Kaushal Kumar Panday
Kaushal Kumar Panday

Reputation: 2467

You can use the JavaScriptSerializer to do this. Assuming

//json contains the JSON Response
var jsonOutput = new System.Web.Script.Serialization.JavaScriptSerializer();
jsonOutput.DeserializeObject(json);

This has been discussed earlier. See this thread: Easiest way to parse JSON response

Refer this link for JavaScriptSerializer: https://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer(v=vs.110).aspx

Upvotes: 0

Related Questions