I have the following JSON to POST to my Web API endpoint.
[
{
"name": "REGION",
"value": ["MA", "SE", "SW"]
}
]
The Web API endpoint is as follows
public class Parameter
{
public string Name { get; set; }
// Value can be string, string[], int, or int[]
public dynamic Value { get; set; }
}
[Route("{chart}/data/")]
[HttpPost]
public IHttpActionResult GetData(string chart, IList<Parameter> parameters)
{
// ... do stuff ...
}
Whenever value in the JSON is an array the deserialized parameter's Value is a JArray rather than an array of string, int, etc. However, if value is a simply a string or number then Value in the deserialized parameters is also a string or a number.
What gives? Why isn't the array in the JSON being deserialized into an array of the proper type?
"value"to be deserialized intoJObjectorJArray, you could manually convert to the conventional .Net types instead. See How do I use JSON.NET to deserialize into nested/recursive Dictionary and List? and JSON.net serializes a json array into a JArray when the destination is an object. How can I change that?. In fact this may be a duplicate of one or both; agree?