I am trying to deserialize JSON with dots in the property names into a key-value format. I am using the inbuilt ASP.NET MVC model binding. It seems to be interpreting the dots as a object notation instead of just a key-value object. Is there any way to make it deserialize correctly as a key value ignoring the dots? This is important as the data will need to be output again in this format.
Controller Action
[HttpPost]
public ActionResult SaveProgress(int id, ProgressVM data)
{
// ProgressVM Data property has an item with key "prop" and a null value, nothing else
}
View model
public class ProgressVM
{
public int ID { get; set; }
public Dictionary<string, string> Data { get; set; }
}
JSON Example
{
"ID": 123,
"Data": {
"prop.0.name": "value",
"prop.0.id": "value",
"prop.1.name": "value",
"prop.2.name": "value",
"prop.3.name": "value"
}
}
I came here looking for a way to deserialize a json string into a model, and while the question here is solved though the MVC framework, it did not solve my issue.
Given a json string of
{
"Property.Something": "The value"
}
I found that using the [JsonProperty] attribute I could deserialize the string into my model like this:
public class JsonModel
{
[JsonProperty(PropertyName = "Property.Something")]
public string PropertySomething {get; set;}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With