Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize JSON with dot in property name

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"
    }
}
like image 305
Jayden Meyer Avatar asked Aug 01 '16 01:08

Jayden Meyer


1 Answers

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;}
}
like image 170
Dave Thompson Avatar answered Sep 29 '22 23:09

Dave Thompson