Apoorv
Apoorv

Reputation: 2043

Parsing JSON object & Array using C# API

I am building up a UWP application that is querying Microsoft Graph API for getting the calander data for a logged in participant. I receive this JSON response

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users()/events(subject,body,bodyPreview,organizer,attendees,start,end,location)",

    "value": [
        {
            "subject": "Lets Party Harder",
            "bodyPreview": "",
            "body": {
                //deleted as its pure HTML Content                },
            "start": {
                "dateTime": "2017-06-14T12:00:00.0000000",
                "timeZone": "UTC"
            },
            "end": {
                "dateTime": "2017-06-14T12:30:00.0000000",
                "timeZone": "UTC"
            },
            "location": {
                "displayName": "",
                "address": {}
            },
            "attendees": [
                {
                    "type": "required",
                    "status": {
                        "response": "none",
                        "time": "0001-01-01T00:00:00Z"
                    },
                    "emailAddress": {
                        "name": "[email protected]",
                        "address": "[email protected]"
                    }
                }
            ],
            "organizer": {
                "emailAddress": {
                    "name": "Apoorv Kumar",
                    "address": "[email protected]"
                }
            }
        },
        {
            "@odata.etag": "W/\"7N0zd4iZvEqXjwOQQ+rwVAAAtC3kEw==\"",
            "id": "AQMkADAwATM3ZmYAZS05ZDYzLWVkZjYtMDACLTAwCgBGAAAD5Yo-MIi8hUKqqYwhg_4WrAcA7N0zd4iZvEqXjwOQQ_rwVAAAAgENAAAA7N0zd4iZvEqXjwOQQ_rwVAAAALQFnyQAAAA=",
            "subject": "All Hands Up",
            "bodyPreview": "Hi",
            "body": {
                //deleted as its pure HTML content},
            "start": {
                "dateTime": "2017-06-14T10:30:00.0000000",
                "timeZone": "UTC"
            },
            "end": {
                "dateTime": "2017-06-14T11:00:00.0000000",
                "timeZone": "UTC"
            },
            "location": {
                "displayName": "",
                "address": {}
            },
            "attendees": [
                {
                    "type": "required",
                    "status": {
                        "response": "none",
                        "time": "0001-01-01T00:00:00Z"
                    },
                    "emailAddress": {
                        "name": "mohit",
                        "address": "[email protected]"
                    }
                },
                {
                    "type": "required",
                    "status": {
                        "response": "none",
                        "time": "0001-01-01T00:00:00Z"
                    },
                    "emailAddress": {
                        "name": "Apoorv K Upadhyay",
                        "address": "[email protected]"
                    }
                }
            ],
            "organizer": {
                "emailAddress": {
                    "name": "Apoorv Kumar",
                    "address": "[email protected]"
                }
            }
        },
        {
                           "subject": "Meeting Reminder",
            "bodyPreview": "REMINDER: To attend your 2016 Annual Health Check, as per your appointment request booking",
            "body": {

            },
            "start": {
                "dateTime": "2017-01-21T03:30:00.0000000",
                "timeZone": "UTC"
            },
            "end": {
                "dateTime": "2017-01-21T03:45:00.0000000",
                "timeZone": "UTC"
            },
            "location": {
                "displayName": "Please refer to mail content "
            },
            "attendees": [
                {
                    "type": "required",
                    "status": {
                        "response": "none",
                        "time": "0001-01-01T00:00:00Z"
                    },
                    "emailAddress": {
                        "name": "[email protected]",
                        "address": "[email protected]"
                    }
                },
                {
                    "type": "required",
                    "status": {
                        "response": "none",
                        "time": "0001-01-01T00:00:00Z"
                    },
                    "emailAddress": {
                        "name": "[email protected]",
                        "address": "[email protected]"
                    }
                }
            ],
            "organizer": {
                "emailAddress": {
                    "name": "[email protected]",
                    "address": "[email protected]"
                }
            }
        },
        nospace\">•</span></td>\r\n<td style=\"padding-right:10px; font-family:Arial,Sans-serif; font-size:13px; color:#222\">\r\n<div>\r\n<div style=\"margin:0 0 0.3em 0\"><span itemprop=\"attendee\" itemscope=\"\" itemtype=\"http://schema.org/Person\"><span itemprop=\"name\" class=\"notranslate\">Akash Mishra</span>\r\n<meta itemprop=\"email\" content=\"[email protected]\">\r\n</span></div>\r\n</div>\r\n</td>\r\n</tr>\r\n<tr>\r\n<td style=\"padding-right:10px; font-family:Arial,Sans-serif; font-size:13px; color:#222\">\r\n<span style=\"font-family:Courier New,monospace\">•</span></td>\r\n<td style=\"padding-right:10px; font-family:Arial,Sans-serif; font-size:13px; color:#222\">\r\n<div>\r\n<div style=\"margin:0 0 0.3em 0\"><span itemprop=\"attendee\" itemscope=\"\" itemtype=\"http://schema.org/Person\"><span itemprop=\"name\" class=\"notranslate\">Kashish Gori</span>\r\n<meta itemprop=\"email\" content=\"[email protected]\">\r\n</span></div>\r\n</div>\r\n</td>\r\n</tr>\r\n<tr>\r\n<td style=\"padding-right:10px; font-family:Arial,Sans-serif; font-size:13px; color:#222\">\r\n<span style=\"font-family:Courier New,monospace\">•</span></td>\r\n<td style=\"padding-right:10px; font-family:Arial,Sans-serif; font-size:13px; color:#222\">\r\n<div>\r\n<div style=\"margin:0 0 0.3em 0\"><span itemprop=\"attendee\" itemscope=\"\" itemtype=\"http://schema.org/Person\"><span itemprop=\"name\" class=\"notranslate\">Vikas Mishra</span>\r\n<meta itemprop=\"email\" content=\"[email protected]\">\r\n</span></div>\r\n</div>\r\n</td>\r\n</tr>\r\n<tr>\r\n<td style=\"padding-right:10px; font-family:Arial,Sans-serif; font-size:13px; color:#222\">\r\n<span style=\"font-family:Courier New,monospace\">•</span></td>\r\n<td style=\"padding-right:10px; font-family:Arial,Sans-serif; font-size:13px; color:#222\">\r\n<div>\r\n<div style=\"margin:0 0 0.3em 0\"><span itemprop=\"attendee\" itemscope=\"\" itemtype=\"http://schema.org/Person\"><span itemprop=\"name\" class=\"notranslate\">[email protected]</span>\r\n<meta itemprop=\"email\" content=\"[email protected]\">\r\n</span></div>\r\n</div>\r\n</td>\r\n</tr>\r\n<tr>\r\n<td style=\"padding-right:10px; font-family:Arial,Sans-serif; font-size:13px; color:#222\">\r\n<span style=\"font-family:Courier New,monospace\">•</span></td>\r\n<td style=\"padding-right:10px; font-family:Arial,Sans-serif; font-size:13px; color:#222\">\r\n<div>\r\n<div style=\"margin:0 0 0.3em 0\"><span itemprop=\"attendee\" itemscope=\"\" itemtype=\"http://schema.org/Person\"><span itemprop=\"name\" class=\"notranslate\">Roopesh Tripathi</span>\r\n<meta itemprop=\"email\" content=\"[email protected]\">\r\n</span></div>\r\n</div>\r\n</td>\r\n</tr>\r\n<tr>\r\n<td style=\"padding-right:10px; font-family:Arial,Sans-serif; font-size:13px; color:#222\">\r\n<span style=\"font-family:Courier New,monospace\">•</span></td>\r\n<td style=\"padding-right:10px; font-family:Arial,Sans-serif; font-size:13px; color:#222\">\r\n<div>\r\n<div style=\"margin:0 0 0.3em 0\"><span itemprop=\"attendee\" itemscope=\"\" itemtype=\"http://schema.org/Person\"><span itemprop=\"name\" class=\"notranslate\">apoorv vyas</span>\r\n<meta itemprop=\"email\" content=\"[email protected]\">\r\n</span></div>\r\n</div>\r\n</td>\r\n</tr>\r\n<tr>\r\n<td style=\"padding-right:10px; font-family:Arial,Sans-serif; font-size:13px; color:#222\">\r\n<span style=\"font-family:Courier New,monospace\">•</span></td>\r\n<td style=\"padding-right:10px; font-family:Arial,Sans-serif; font-size:13px; color:#222\">\r\n<div>\r\n<div style=\"margin:0 0 0.3em 0\"><span itemprop=\"attendee\" itemscope=\"\" itemtype=\"http://schema.org/Person\"><span itemprop=\"name\" class=\"notranslate\">[email protected]</span>\r\n<meta itemprop=\"email\" content=\"[email protected]\">\r\n</span></div>\r\n</div>\r\n</td>\r\n</tr>\r\n</tbody>\r\n</table>\r\n</td>\r\n</tr>\r\n</tbody>\r\n</table>\r\n</div>\r\n<p style=\"color:#222; font-size:13px; margin:0\"><span style=\"color:#888\">Going? </span>\r\n<wbr><strong><span itemprop=\"potentialaction\" itemscope=\"\" itemtype=\"http://schema.org/RsvpAction\">\r\n<meta itemprop=\"attendance\" content=\"http://schema.org/RsvpAttendance/Yes\">\r\n<span itemprop=\"handler\" itemscope=\"\" itemtype=\"http://schema.org/HttpActionHandler\"><link itemprop=\"method\" href=\"http://schema.org/HttpRequestMethod/GET\"><a href=\"https://www.google.com/calendar/event?action=RESPOND&amp;eid=bGdiMjFuY3I2MmZ0OWp1bW1wbmJwZGhhb2MgYXBvb3J2LnVwYWRoeWF5QGxpdmUuaW4&amp;rst=1&amp;tok=MjIjbGVhcm5iYXl0ZWNoQGdtYWlsLmNvbWQ5NDQxODM1YTNiZmRjYTU4NDk4ZWIwYWY3YWYyNWQ5YTI2MDBlNDc&amp;ctz=Asia/Calcutta&amp;hl=en\" itemprop=\"url\" style=\"color:#20c; white-space:nowrap\">Yes</a></span></span><span style=\"margin:0 0.4em; font-weight:normal\">\r\n - </span><span itemprop=\"potentialaction\" itemscope=\"\" itemtype=\"http://schema.org/RsvpAction\">\r\n<meta itemprop=\"attendance\" content=\"http://schema.org/RsvpAttendance/Maybe\">\r\n<span itemprop=\"handler\" itemscope=\"\" itemtype=\"http://schema.org/HttpActionHandler\"><link itemprop=\"method\" href=\"http://schema.org/HttpRequestMethod/GET\"><a href=\"https://www.google.com/calendar/event?action=RESPOND&amp;eid=bGdiMjFuY3I2MmZ0OWp1bW1wbmJwZGhhb2MgYXBvb3J2LnVwYWRoeWF5QGxpdmUuaW4&amp;rst=3&amp;tok=MjIjbGVhcm5iYXl0ZWNoQGdtYWlsLmNvbWQ5NDQxODM1YTNiZmRjYTU4NDk4ZWIwYWY3YWYyNWQ5YTI2MDBlNDc&amp;ctz=Asia/Calcutta&amp;hl=en\" itemprop=\"url\" style=\"color:#20c; white-space:nowrap\">Maybe</a></span></span><span style=\"margin:0 0.4em; font-weight:normal\">\r\n - </span><span itemprop=\"potentialaction\" itemscope=\"\" itemtype=\"http://schema.org/RsvpAction\">\r\n<meta itemprop=\"attendance\" content=\"http://schema.org/RsvpAttendance/No\">\r\n<span itemprop=\"handler\" itemscope=\"\" itemtype=\"http://schema.org/HttpActionHandler\"><link itemprop=\"method\" href=\"http://schema.org/HttpRequestMethod/GET\"><a href=\"https://www.google.com/calendar/event?action=RESPOND&amp;eid=bGdiMjFuY3I2MmZ0OWp1bW1wbmJwZGhhb2MgYXBvb3J2LnVwYWRoeWF5QGxpdmUuaW4&amp;rst=2&amp;tok=MjIjbGVhcm5iYXl0ZWNoQGdtYWlsLmNvbWQ5NDQxODM1YTNiZmRjYTU4NDk4ZWIwYWY3YWYyNWQ5YTI2MDBlNDc&amp;ctz=Asia/Calcutta&amp;hl=en\" itemprop=\"url\" style=\"color:#20c; white-space:nowrap\">No</a></span></span></strong>\r\n<wbr><a href=\"https://www.google.com/calendar/event?action=VIEW&amp;eid=bGdiMjFuY3I2MmZ0OWp1bW1wbmJwZGhhb2MgYXBvb3J2LnVwYWRoeWF5QGxpdmUuaW4&amp;tok=MjIjbGVhcm5iYXl0ZWNoQGdtYWlsLmNvbWQ5NDQxODM1YTNiZmRjYTU4NDk4ZWIwYWY3YWYyNWQ5YTI2MDBlNDc&amp;ctz=Asia/Calcutta&amp;hl=en\" itemprop=\"url\" style=\"color:#20c; white-space:nowrap\">more\r\n options »</a></p>\r\n</td>\r\n</tr>\r\n<tr>\r\n<td style=\"background-color:#f6f6f6; color:#888; border-top:1px Solid #ccc; font-family:Arial,Sans-serif; font-size:11px\">\r\n<p>Invitation from <a href=\"https://www.google.com/calendar/\" target=\"_blank\" style=\"\">\r\nGoogle Calendar</a></p>\r\n<p>You are receiving this courtesy email at the account [email protected] because you are an attendee of this event.</p>\r\n<p>To stop receiving future updates for this event, decline this event. Alternatively you can sign up for a Google account at https://www.google.com/calendar/ and control your notification settings for your entire calendar.</p>\r\n<p>Forwarding this invitation could allow any recipient to modify your RSVP response.\r\n<a href=\"https://support.google.com/calendar/answer/37135#forwarding\">Learn More</a>.</p>\r\n</td>\r\n</tr>\r\n</tbody>\r\n</table>\r\n</div>\r\n</span></span>\r\n</body>\r\n</html>\r\n"
            },
            "start": {
                "dateTime": "2017-01-14T04:30:00.0000000",
                "timeZone": "UTC"
            },
            "end": {
                "dateTime": "2017-01-14T07:00:00.0000000",
                "timeZone": "UTC"
            },
            "location": {
                "displayName": "Learnbay:Data Structures and Algorithms,Python,Embedded Systems Training, Learnbay,19/1,2nd Floor, Classic Aura(Beside Aricent)BLOCK 12, Marathahalli - Sarjapur Outer Ring Road, Kaverappa Layout, Kadubeesanahalli, Chandana, Bengaluru, Karnataka 560103, India"
            },
            "attendees": [
                {
                    "type": "required",
                    "status": {
                        "response": "none",
                        "time": "0001-01-01T00:00:00Z"
                    },
                    "emailAddress": {
                        "name": "[email protected]",
                        "address": "[email protected]"
                    }
                },
                {
                    "type": "required",
                    "status": {
                        "response": "none",
                        "time": "0001-01-01T00:00:00Z"
                    },
                    "emailAddress": {
                        "name": "Mohit Jain",
                        "address": "[email protected]"
                    }
                },
                {
                    "type": "required",
                    "status": {
                        "response": "none",
                        "time": "0001-01-01T00:00:00Z"
                    },
                    "emailAddress": {
                        "name": "[email protected]",
                        "address": "[email protected]"
                    }
                },
                {
                    "type": "required",
                    "status": {
                        "response": "none",
                        "time": "0001-01-01T00:00:00Z"
                    },
                    "emailAddress": {
                        "name": "Nancy Goel",
                        "address": "[email protected]"
                    }
                },
                {
                    "type": "required",
                    "status": {
                        "response": "none",
                        "time": "0001-01-01T00:00:00Z"
                    },
                    "emailAddress": {
                        "name": "Tejas Bargal",
                        "address": "[email protected]"
                    }
                },
                {
                    "type": "required",
                    "status": {
                        "response": "none",
                        "time": "0001-01-01T00:00:00Z"
                    },
                    "emailAddress": {
                        "name": "abhishek anand",
                        "address": "[email protected]"
                    }
                },
                {
                    "type": "required",
                    "status": {
                        "response": "none",
                        "time": "0001-01-01T00:00:00Z"
                    },
                    "emailAddress": {
                        "name": "sriharitej kolagani",
                        "address": "[email protected]"
                    }
                },
                {
                    "type": "required",
                    "status": {
                        "response": "none",
                        "time": "0001-01-01T00:00:00Z"
                    },
                    "emailAddress": {
                        "name": "RAMMOHAN CIRIGIRI",
                        "address": "[email protected]"
                    }
                },
                {
                    "type": "required",
                    "status": {
                        "response": "none",
                        "time": "0001-01-01T00:00:00Z"
                    },
                    "emailAddress": {
                        "name": "Parag Sawant",
                        "address": "[email protected]"
                    }
                },
                {
                    "type": "required",
                    "status": {
                        "response": "none",
                        "time": "0001-01-01T00:00:00Z"
                    },
                    "emailAddress": {
                        "name": "[email protected]",
                        "address": "[email protected]"
                    }
                },
                {
                    "type": "required",
                    "status": {
                        "response": "none",
                        "time": "0001-01-01T00:00:00Z"
                    },
                    "emailAddress": {
                        "name": "Abhishek Gupta",
                        "address": "[email protected]"
                    }
                },
                {
                    "type": "required",
                    "status": {
                        "response": "none",
                        "time": "0001-01-01T00:00:00Z"
                    },
                    "emailAddress": {
                        "name": "Naveen G",
                        "address": "[email protected]"
                    }
                },
                {
                    "type": "required",
                    "status": {
                        "response": "none",
                        "time": "0001-01-01T00:00:00Z"
                    },
                    "emailAddress": {
                        "name": "Neeraj Yadav",
                        "address": "[email protected]"
                    }
                },
                {
                    "type": "required",
                    "status": {
                        "response": "none",
                        "time": "0001-01-01T00:00:00Z"
                    },
                    "emailAddress": {
                        "name": "Akash Mishra",
                        "address": "[email protected]"
                    }
                },
                {
                    "type": "required",
                    "status": {
                        "response": "none",
                        "time": "0001-01-01T00:00:00Z"
                    },
                    "emailAddress": {
                        "name": "Kashish Gori",
                        "address": "[email protected]"
                    }
                },
                {
                    "type": "required",
                    "status": {
                        "response": "none",
                        "time": "0001-01-01T00:00:00Z"
                    },
                    "emailAddress": {
                        "name": "Vikas Mishra",
                        "address": "[email protected]"
                    }
                },
                {
                    "type": "required",
                    "status": {
                        "response": "none",
                        "time": "0001-01-01T00:00:00Z"
                    },
                    "emailAddress": {
                        "name": "[email protected]",
                        "address": "[email protected]"
                    }
                },
                {
                    "type": "required",
                    "status": {
                        "response": "none",
                        "time": "0001-01-01T00:00:00Z"
                    },
                    "emailAddress": {
                        "name": "Roopesh Tripathi",
                        "address": "[email protected]"
                    }
                },
                {
                    "type": "required",
                    "status": {
                        "response": "none",
                        "time": "0001-01-01T00:00:00Z"
                    },
                    "emailAddress": {
                        "name": "apoorv vyas",
                        "address": "[email protected]"
                    }
                },
                {
                    "type": "required",
                    "status": {
                        "response": "none",
                        "time": "0001-01-01T00:00:00Z"
                    },
                    "emailAddress": {
                        "name": "[email protected]",
                        "address": "[email protected]"
                    }
                }
            ],
            "organizer": {
                "emailAddress": {
                    "name": "[email protected]",
                    "address": "[email protected]"
                }
            }
        },
                ]
}

I am looking into a solution where I can get list of all the meetings available with the start and end time , meeting details, no of participants as a list with their email IDs, Organizer name and email id.

I generated few classes for parsing JSON

public class Body
{
    public string contentType { get; set; }
    public string content { get; set; }
}

public class Start
{
    public string dateTime { get; set; }
    public string timeZone { get; set; }
}

public class End
{
    public string dateTime { get; set; }
    public string timeZone { get; set; }
}

public class Address
{
}

public class Location
{
    public string displayName { get; set; }
    public Address address { get; set; }
}

public class EmailAddress
{
    public string name { get; set; }
    public string address { get; set; }
}

public class Organizer
{
    public EmailAddress emailAddress { get; set; }
}

public class Value
{
    public string [email protected] { get; set; }
    public string id { get; set; }
    public string subject { get; set; }
    public string bodyPreview { get; set; }
    public Body body { get; set; }
    public Start start { get; set; }
    public End end { get; set; }
    public Location location { get; set; }
    public List<object> attendees { get; set; }
    public Organizer organizer { get; set; }
}

public class RootObject
{
    public string [email protected] { get; set; }
    public string [email protected] { get; set; }
    public List<Value> value { get; set; }
}

I tried to use the NewtonSoft library and doing something like this but getting null exception.

JObject root = JObject.Parse(content);
                JArray ValueArray = (JArray)root["value"];

Upvotes: 0

Views: 1540

Answers (1)

Mohit S
Mohit S

Reputation: 14064

So with the updated link of the JSON your class should look like this

public class Body
{

    [JsonProperty("contentType")]
    public string contentType
    {
        get;
        set;
    }

    [JsonProperty("content")]
    public string content
    {
        get;
        set;
    }
}

public class Start
{

    [JsonProperty("dateTime")]
    public DateTime dateTime
    {
        get;
        set;
    }

    [JsonProperty("timeZone")]
    public string timeZone
    {
        get;
        set;
    }
}

public class End
{

    [JsonProperty("dateTime")]
    public DateTime dateTime
    {
        get;
        set;
    }

    [JsonProperty("timeZone")]
    public string timeZone
    {
        get;
        set;
    }
}

public class Address
{
}

public class Location
{

    [JsonProperty("displayName")]
    public string displayName
    {
        get;
        set;
    }

    [JsonProperty("address")]
    public Address address
    {
        get;
        set;
    }
}

public class Status
{

    [JsonProperty("response")]
    public string response
    {
        get;
        set;
    }

    [JsonProperty("time")]
    public DateTime time
    {
        get;
        set;
    }
}

public class EmailAddress
{

    [JsonProperty("name")]
    public string name
    {
        get;
        set;
    }

    [JsonProperty("address")]
    public string address
    {
        get;
        set;
    }
}

public class Attendee
{

    [JsonProperty("type")]
    public string type
    {
        get;
        set;
    }

    [JsonProperty("status")]
    public Status status
    {
        get;
        set;
    }

    [JsonProperty("emailAddress")]
    public EmailAddress emailAddress
    {
        get;
        set;
    }
}

public class Organizer
{

    [JsonProperty("emailAddress")]
    public EmailAddress emailAddress
    {
        get;
        set;
    }
}

public class Value
{

    [JsonProperty("@odata.etag")]
    public string odataetag
    {
        get;
        set;
    }

    [JsonProperty("id")]
    public string id
    {
        get;
        set;
    }

    [JsonProperty("subject")]
    public string subject
    {
        get;
        set;
    }

    [JsonProperty("bodyPreview")]
    public string bodyPreview
    {
        get;
        set;
    }

    [JsonProperty("body")]
    public Body body
    {
        get;
        set;
    }

    [JsonProperty("start")]
    public Start start
    {
        get;
        set;
    }

    [JsonProperty("end")]
    public End end
    {
        get;
        set;
    }

    [JsonProperty("location")]
    public Location location
    {
        get;
        set;
    }

    [JsonProperty("attendees")]
    public IList<Attendee> attendees
    {
        get;
        set;
    }

    [JsonProperty("organizer")]
    public Organizer organizer
    {
        get;
        set;
    }
}

public class Rootobject
{

    [JsonProperty("@odata.context")]
    public string odatacontext
    {
        get;
        set;
    }

    [JsonProperty("@odata.nextLink")]
    public string odatanextLink
    {
        get;
        set;
    }

    [JsonProperty("value")]
    public IList<Value> value
    {
        get;
        set;
    }
}

Next use NewtonSoft Library to get your results. like this

var items = JsonConvert.DeserializeObject<Rootobject>(jsonstr);

I am also attaching a Screenshot to see the output

Have a look

Update

var sub = items.value[0].subject;

will extract the subject from the Json Object items.

Upvotes: 3

Related Questions