Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing Json into a dynamic c# object with a dynamic key

I'm trying to parse this Google calendar response I'm getting from their Rest API using c#, but I seem to keep getting stuck. [edited] Update, the @ symbol isn't preventing the drill down, I verified by replacing the @ with _at_. See the screenshot of the Quick watch: enter image description here

I'm sure I'm accessing this incorrectly...

Here's the jsonString I'm trying to parse:

{
 "kind": "calendar#freeBusy",
 "timeMin": "2015-06-12T14:00:00.000Z",
 "timeMax": "2015-06-14T14:00:00.000Z",
 "calendars": {
  "[email protected]": {
   "busy": [
    {
     "start": "2015-06-13T18:30:00Z",
     "end": "2015-06-13T19:30:00Z"
    },
    {
     "start": "2015-06-13T20:30:00Z",
     "end": "2015-06-13T21:30:00Z"
    },
    {
     "start": "2015-06-13T23:00:00Z",
     "end": "2015-06-14T00:00:00Z"
    }
   ]
  }
 }
}

I've tried using:

dynamic myObj = Json.Decode(jsonString);

and

var myObj = JsonConvert.DeserializeObject(jsonString);

but I can't figure out how to get into the [email protected] key (which is dynamic based on what I send up) to cycle through all the busy times.

Ideas?

enter image description here

like image 513
user3491101 Avatar asked Jun 13 '15 20:06

user3491101


1 Answers

You can access it via a string indexer:

var myObj = JsonConvert.DeserializeObject<dynamic>(jsonString);
Console.WriteLine(myObj.calendars["[email protected]"]);
like image 194
David Deutsch Avatar answered Nov 10 '22 15:11

David Deutsch