We have a JSON object with one of the object having a dash in its name. Ex below.
{ "veg": [ { "id": "3", "name": "Vegetables", "count": "25" }, { "id": "4", "name": "Dal", "count": "2" }, { "id": "5", "name": "Rice", "count": "8" }, { "id": "7", "name": "Breads", "count": "6" }, { "id": "9", "name": "Meals", "count": "3" }, { "id": "46", "name": "Extras", "count": "10" } ], "non-veg": [ { "id": "25", "name": "Starters", "count": "9" }, { "id": "30", "name": "Gravies", "count": "13" }, { "id": "50", "name": "Rice", "count": "4" } ] }
How can we deserialize this json?
Newtonsoft. Json uses reflection to get constructor parameters and then tries to find closest match by name of these constructor parameters to object's properties. It also checks type of property and parameters to match. If there is no match found, then default value will be passed to this parameterized constructor.
Return ValueThe deserialized object from the JSON string.
Provides methods for converting between . NET types and JSON types.
To answer the question on how to do this WITH NewtonSoft, you would use the JsonProperty property attribute flag.
[JsonProperty(PropertyName="non-veg")] public string nonVeg { 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