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

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


You can access it via a string indexer:

var myObj = JsonConvert.DeserializeObject<dynamic>(jsonString);Console.WriteLine(myObj.calendars["joe@bobs.com"]);


I have dealt with a similar problem in the past, however mine was a matter of hyphen, i simply replaced hyphen with underscore. you could potentially do something similar however seeing that it's a e-mail address, it might be a better to modify the schema (regular-expression seeing that you receive json from a third party API) create a new key "mail" so that you can ensure that you keep the original email address intact.

But perhaps more importantly, as you query this API perhaps you already knew the email, if so you could simply do a regex replace:

        string json = '... {"joe@bobs.com":...}...';        Regex regex = new Regex(@"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b");        string jsonfixed = regex.Replace(json, "email");