0

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.

2 Answers 2

3

the error message is correct. Drill down into the data a little... You have:

    "0": {
        "label": "Period",
        "start_date": "2015-04-20",
        "end_date": "2015-04-29"
    }

And you try to put it into:

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; }
}

So more specifically, this JSON:

    {
        "label": "Period",
        "start_date": "2015-04-20",
        "end_date": "2015-04-29"
    }

Is being deserialized to a Dictionary<string,Period>


So then the first property:

        "label": "Period"

Needs to be deserialized to a <string, Period> pair. The left side "label" is properly converted to a string but the right side is a string "Period", and that string can not be deserialized into the class type Period

Hence the error

Error converting value "Period" to type 'CMS.WebApi.Controllers.ActivitiesController+Period'

It is trying to convert the string "Period" to the class Period.


It isn't totally clear what you want to do here, but if there is only ever a "0" in periods then you could just change the property to:

public class Periods
{
    [JsonProperty("0")]
    public Period period { get; set; }
}

But my guess is that you would want to have more than 1 period, so you would have "0", "1", "2" ... etc.

Without putting together some code to test it, I'm not sure if there is a good way to do that.

You might want to try making the PeriodsModel have:

    [JsonProperty("periods")]
    public Dictionary<string, Period> periods { get; set; }

and then you wouldn't need a Periods class at all.

Though usually when I have a variable number of things to pass, I just use a JSON array instead, like:

{
    "id": "13",
    "title": "party",
    "periods": [
        {
            "label": "Period1",
            "start_date": "2015-04-20",
            "end_date": "2015-04-29"
        }, {
            "label": "Period2",
            "start_date": "2015-04-20",
            "end_date": "2015-04-29"
        }, {
            "label": "Period3",
            "start_date": "2015-04-20",
            "end_date": "2015-04-29"
        }
    ]
}
Sign up to request clarification or add additional context in comments.

5 Comments

Yes, I would also like to have an array of objects instead. But I have nothing to do how the Json is formated in the frontend, im only suppose to handle it on server side hence my problem.
@grimsan55 In that case I would try changing the PeriodsModel to have the property: Dictionary<string, Period> periods { get; set; } instead, and see if that works.
Yes that works, but how do I access lets say the end_date in an easy way ?
@grimsan55 I think this would work: model.periods["0"].start_date because model.periods is a Dictionary with the keys "0", "1", "2", etc... so you would just access the dictionary as usual.
Thanks, dunno why I couldn't figure it out.. I'll mark your answer as the correct one since you gave such a detailed explanation even tho Asad gave the correct answer as well.
1

This is how you could do it, since the "0" is more of a key and not a property variable:

public class PeriodsModel
{
    [JsonProperty("id")]
    public int id { get; set; }

    [JsonProperty("title")]
    public string title { get; set; }

    [JsonProperty("periods")]
    public Dictionary<string, Period> periods { 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; }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.