This sort of API response is a nightmare to work with, but seems to be quite popular amongst the loosely-typed language crowd.
To read it in C#, you will need a custom JSON converter. For example:
public class OutputInfo
{
public string status_id { get; set; }
public string status_msg { get; set; }
public string unique_trans_id { get; set; }
public InfoDetailsOrErrorList info { get; set; }
}
[JsonConverter(typeof(InfoDetailsOrErrorListConverter))]
public class InfoDetailsOrErrorList
{
public string case_id { get; set; }
public string[] errors { get; set; }
public sealed class InfoDetailsOrErrorListConverter : JsonConverter<InfoDetailsOrErrorList>
{
public override CanWrite => false;
public override InfoDetailsOrErrorList ReadJson(
JsonReader reader,
Type objectType,
InfoDetailsOrErrorList existingValue,
bool hasExistingValue,
JsonSerializer serializer)
{
switch (reader.TokenType)
{
case JsonToken.Null:
{
return null;
}
case JsonToken.StartArray:
{
string[] errors = serializer.Deserialize<string[]>(reader);
return new InfoDetailsOrErrorList { errors = errors };
}
case JsonToken.StartObject:
{
var temp = new InfoDetailsOrErrorList();
serializer.Populate(reader, temp);
return temp;
}
default:
{
throw new JsonException();
}
}
}
public override void WriteJson(
JsonWriter writer,
InfoDetailsOrErrorList value,
JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
}
Unfortunately, your consuming code will now need to check whether the
info
property contains an
errors
array
(cases 2 and 3) or a
case_id
(case 1).