I am consuming an api with .NET Core, which returns arrays of objects. But when the array contains only one object, the external API will remove the array, and paste the object directly.
I hope there is an attribute or setting which does not require me to work with JObjects, or override the JsonConverter.
Sample JSON im dealing with:
{
"a": {
"b": [{
"id": 1,
"name": "first object"
}, {
"id": 2,
"name": "second object"
}]
}
}
Json with omitted array
{
"a": {
"b": {
"id": 1,
"name": "One object only"
}
}
}
This is what I am doing (simplified)
public class Response
{
public A a { get; set; }
}
public class A
{
public List<B> b { get; set; }
}
public class B
{
public int id { get; set; }
public string name { get; set; }
}
var apiResponse = await response.Content.ReadAsAsync<Response>()
I wish it would be possible that when the second JSON example is returned, that ReadAsAsync() would automatically understand that it should be converted to an array with one object of B.
The problem is that this can happen on many places in the API responses.