using System.Collections.Generic;
using Newtonsoft.Json.Linq;
public static void Main()
{""name"": ""Raven"", ""wings"": 2},
{""name"": ""Wolf"", ""wings"": 0}
{""type"": ""Car"", ""wheels"": 4},
{""type"": ""Motorcycle"", ""wheels"": 2},
{""type"": ""Boat"", ""wheels"": 0}
var animalsResponse = JsonConvert.DeserializeObject<CollectionResponse<Animal>>(animalsJson);
Console.WriteLine($"There are {animalsResponse.Count} animals");
foreach (Animal animal in animalsResponse.Items)
Console.WriteLine($"A {animal.Name} has {animal.Wings} wings");
var vehiclesResponse = JsonConvert.DeserializeObject<CollectionResponse<Vehicle>>(vehiclesJson);
Console.WriteLine($"There are {vehiclesResponse.Count} animals");
foreach (Vehicle vehicle in vehiclesResponse.Items)
Console.WriteLine($"A {vehicle.Type} has {vehicle.Wheels} wheels");
private static void DeserializeAndDump<T>(string json)
public string Name { get; set; }
public int Wings { get; set; }
public string Type { get; set; }
public int Wheels { get; set; }
[JsonConverter(typeof(CollectionJsonConverter))]
class CollectionResponse<T>
public List<T> Items { get; set; }
public int Count { get; set; }
class CollectionJsonConverter : JsonConverter
public override bool CanConvert(Type objectType)
return objectType.IsGenericType &&
objectType.GetGenericTypeDefinition() == typeof(CollectionResponse<>);
public override object ReadJson(JsonReader reader,
Type objectType, object existingValue, JsonSerializer serializer)
var obj = JObject.Load(reader);
var itemsProp = obj.Properties().Single(p => p.Value.Type == JTokenType.Array);
itemsProp.Replace(new JProperty("items", itemsProp.Value));
var instance = Activator.CreateInstance(objectType);
serializer.Populate(obj.CreateReader(), instance);
public override bool CanWrite => false;
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
throw new NotImplementedException();