There are websites that will generate classes for you from JSON data. My favourite is:
JSON Utils: Generate C#, VB.Net, SQL Table, Java and PHP from JSON[
^]
Using the above JSON data, the following classes are generated:
public class Field
{
[JsonProperty("value")]
public string Value { get; set; }
[JsonProperty("order")]
public int Order { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("maxlen")]
public string Maxlen { get; set; }
[JsonProperty("required")]
public string Required { get; set; }
[JsonProperty("label")]
public string Label { get; set; }
[JsonProperty("time")]
public string Time { get; set; }
[JsonProperty("date")]
public string Date { get; set; }
}
public class Hidden
{
}
public class Example
{
[JsonProperty("fields")]
public IList<Field> Fields { get; set; }
[JsonProperty("action")]
public string Action { get; set; }
[JsonProperty("hidden")]
public Hidden Hidden { get; set; }
[JsonProperty("display")]
public string Display { get; set; }
}
Next you will need to write code to convert / map the JSON data to the class structure.
NuGet Gallery | Json.NET 10.0.3[
^] is used by most.
Once referenced in your project, here is a helper class that I use to do the conversion from/to classes:
public static class JsonConverter
{
public static string FromClass<T>(T data, bool isEmptyToNull = false, JsonSerializerSettings jsonSettings = null)
{
string response = string.Empty;
if (!EqualityComparer<T>.Default.Equals(data, default(T)))
response = JsonConvert.SerializeObject(data, jsonSettings);
return isEmptyToNull ? (response == "{}" ? "null" : response) : response;
}
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;
}
}
And this is how to use the helper class:
var rawJson = "{\"fields\": [{\"value\": \"3732\",\"order\": 1,\"name\": \"id\",\"type\": \"hidden\"},{\"maxlen\": \"13\",\"required\": \"\",\"value\": \"0\",\"order\": 2,\"name\": \"job\",\"label\": \"Job \",\"type\": \"text\"},{\"maxlen\": \"13\",\"required\": \"\",\"value\": \"0\",\"order\": 3,\"name\": \"client\",\"label\": \"Client \",\"type\": \"text\"},{\"maxlen\": \"50\",\"required\": \"required\",\"value\": \"Tim Nguyen\",\"order\": 4,\"name\": \"task_name\",\"label\": \"Task Name \",\"type\": \"text\"},{\"required\": \"required\",\"time\": \"05:30 PM\",\"date\": \"07/18/2017\",\"order\": 5,\"name\": \"start_date\",\"label\": \"Start Date \",\"type\": \"datetime\"},{\"required\": \"\",\"time\": \"07:00 PM\",\"date\": \"07/18/2017\",\"order\": 6,\"name\": \"end_date\",\"label\": \"End Date \",\"type\": \"datetime\"},{\"required\": \"\",\"value\": \"Roof Inspection\",\"order\": 7,\"name\": \"task_description\",\"label\": \"Task Description \",\"type\": \"textareainput\"}],\"action\": \"update_task\",\"hidden\": {},\"display\": \"Update Task\"}";
var result = JsonConverter.ToClass<Example>(rawJson);