I have the following JSON object which i get to my Web API controller:
{
"id": "13",
"title": "party",
"periods": {
"0": {
"label": "Period",
"start_date": "2015-04-20",
"end_date": "2015-04-29"
}
}
}
I want to try to Deserialize this straight into a Model that i have in C# but I'm not able too.
Here is my Model:
public class PeriodsModel
{
[JsonProperty("id")]
public int id { get; set; }
[JsonProperty("title")]
public string title { get; set; }
[JsonProperty("periods")]
public Periods periods { get; set; }
}
public class Periods
{
[JsonProperty("0")]
public Dictionary<string,Period> period { get; set; }
}
public class Period
{
[JsonProperty("label")]
public string label { get; set; }
[JsonProperty("start_date")]
public string start_date { get; set; }
[JsonProperty("end_date")]
public string end_date { get; set; }
}
and here is my method in my controller:
public void Put([FromBody]JToken jsonbody)
{
var myJsonObject = JsonConvert.SerializeObject(jsonbody);
PeriodsModel model = JsonConvert.DeserializeObject<PeriodsModel>(myJsonObject);
}
Here is my error msg that i get:
An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code
Additional information: Error converting value "Period" to type 'CMS.WebApi.Controllers.ActivitiesController+Period'. Path 'periods.0.label', line 1, position 62.