I receive the following JSON from a Web API that lists the errors that were found in a POST. There can be more than one key-value pair in ModelState
, depending how many errors were found. The only problem is that there are square brackets around the values. So when I deserialize with JSON.net there is an Unexpected token
error.
My solution now is to do a search and replace for those brackets and then deserialize, which does work. But is there a better solution?
My class
public class Error
{
public string Message { get; set; }
public Dictionary<string, string> ModelState { get; set; }
}
The JSON
{
"Message": "The request is invalid.",
"ModelState": {
"member.Gender": ["An error has occurred."],
"member.MemberID": ["The MemberID field is required."],
"member.BoardMemberID": ["The BoardMemberID field is required."],
}
}
How I deserialize now
Error error = JsonConvert.DeserializeObject<Error>(jsonString.Replace("[", "").Replace("]", ""));