I’m trying to figure out a way to parse JSON error messages in C#.
I can receive a response that looks like this:
{
"errors": {
"title": ["can't be blank"]
}
}
or
{
"errors": {
"inventory_policy": ["is not included in the list"]
}
}
or maybe I will receive more than one error in the response.
How would I be able to parse that kind of dynamic response in C#? The key is different for each error message. Could I deserialize it to an object that just has a Dictionary of strings called errors?
3
Answers
you can desirialize json to a dynamic variable and access to property which are in the object
Try this
JObject class has an internal dictionary of those properties. Instances of it can be enumerated, and each of the child object can be accessed. Here is a sample code:
The JObject class is defined in the Newtonsoft.Json package, and to reference it from your project you’ll need to add the following package reference in your csproj file (or just do that yourself using Nuget Package Manager):
Hope this will help!