What is the best way to deserialize the following JSON response into a generic object? For example I would like to access the response message and a the list of errors (and accessing the field name and error message of it).
{
"message": "The given data was invalid.",
"errors": {
"name": [
"Name is required."
],
"gender": [
"Gender is required."
],
"date_of_birth": [
"Date of birth is required."
]
}
}
Edit:
I would like to access the JSON object in a way something like this
string message = genericObject.message
foreach (error errorElement in genericObject.errors)
{
string errorField = errorElement.fieldName;
string errorDescription = errorElement.errorMessage;
}
Edit 2:
I don't know the possible error fields beforehand.



<T>, can't you simply tell us which objects are involved? If you mean something likedynamic, please say so.public class Response { public string Message { get; set; } public Dictionary<string, List<string>> Errors { get; } = new Dictionary<string, List<string>>(); }dynamic. I think what you want is this: stackoverflow.com/questions/2546138/…. Ignore the part of the question that sounds different from yours and look at the answers.JsonConvert.DeserializeObject<Response>(json).