The problem that you are having is that you have complex node names that don't directly translate to .Net variable names. So Dynamic object creation is failing.
Examples of this are:
vehicle-model
\"track-pack\"-gauges
3xlock-start-from-oem-remote
All are invalid names .Net names but valid Json Node names.
This does not mean that it is not possible to do, it means that you have to do it manually.
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
namespace JsonConverter2
{
class Program
{
static void Main(string[] args)
{
string raw = File.ReadAllText("data.json");
var data = JsonHelper.ToClass<Response>(raw);
Debugger.Break();
}
}
public class VehicleMake
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("path")]
public string Path { get; set; }
}
public class VehicleYear
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("path")]
public string Path { get; set; }
}
public class VehicleModel
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("path")]
public string Path { get; set; }
}
public class Product
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("path")]
public string Path { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("category")]
public string Category { get; set; }
}
public class Coverage
{
[JsonProperty("t-harness-support")]
public bool THarnessSupport { get; set; }
[JsonProperty("\"track-pack\"-gauges")]
public bool TrackPackGauges { get; set; }
[JsonProperty("2-way-serial-data-communication")]
public bool AA2WaySerialDataCommunication { get; set; }
[JsonProperty("3xlock-start-from-oem-remote")]
public bool AA3xlockStartFromOemRemote { get; set; }
[JsonProperty("3xlock-start-w-aftermarket-starter")]
public bool AA3xlockStartWAftermarketStarter { get; set; }
}
public class Response
{
[JsonProperty("vehicle-make")]
public VehicleMake VehicleMake { get; set; }
[JsonProperty("vehicle-year")]
public VehicleYear VehicleYear { get; set; }
[JsonProperty("vehicle-model")]
public VehicleModel VehicleModel { get; set; }
[JsonProperty("product")]
public Product Product { get; set; }
[JsonProperty("coverage")]
public Coverage Coverage { get; set; }
[JsonProperty("accessories")]
public IList<object> Accessories { get; set; }
}
public static class JsonHelper
{
public static T ToClass<T>(string data, JsonSerializerSettings jsonSettings = null)
{
var response = default(T);
if (!string.IsNullOrEmpty(data))
response = jsonSettings == null
? JsonConvert.DeserializeObject<T>(data)
: JsonConvert.DeserializeObject<T>(data, jsonSettings);
return response;
}
}
}