Question

I cannot figure out why my model will not get populated with the data from my JSON string. Here is the model:

public class MidasReturnModel
{

    public string status { get; set; }

    public string msg { get; set; }

}

And here is my C# code:

MidasReturnModel rtn = JsonConvert.DeserializeObject<MidasReturnModel>(post_responseTemp);

And here is the JSON string post_responseTemp as it gets passed in to that function:

        "{\"MidasReturnModel\": {\"status\":\"warn\", \"msg\":\"Customer does not have contract for this season\"}}"

Can anyone see what I am doing wrong? "rtn" is a Model with both status and msg being "null" when I run through the code.

Was it helpful?

Solution

Looking at your json, you are defining the MidasReturnModel in it.

When this is getting deserialized, it is looking for a property called MidasReturnModel on your class.

I would try your json as:

{
    "status": "warn",
    "msg": "Customer does not have contract for this season"
}

If you are wanting to keep your json the same, then it would have to be a case of wrapping your viewmodel in another class.

public class MidasWrapper
{
    public MidasReturnModel MidasReturnModel { get; set; }
}

public class MidasReturnModel
{

    public string status { get; set; }

    public string msg { get; set; }

}

var rtn = JsonConvert.DeserializeObject<MidasWrapper>(post_responseTemp);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top