0

I am working with a bunch of JSON files that I need to deserialize . A proper I am facing is that some of the Content in the JSON files do not follow proper coding standards , hence when I create the POCO classes I am violating naming conventions

Below is a small example

{
    "category": "classified"
}

In this case I would have to create a POCO class as

public class Category
{
    public string category{ get; set; }
}

here I am starting a property name in a simple letter which is bad naming convention in C#.

Anything I can do about that ?

Devss
  • 67
  • 7
  • Does this answer your question? [How can I change property names when serializing with Json.net?](https://stackoverflow.com/questions/8796618/how-can-i-change-property-names-when-serializing-with-json-net) – xdtTransform Feb 05 '20 at 14:50
  • https://stackoverflow.com/questions/15915503/net-newtonsoft-json-deserialize-map-to-a-different-property-name – xdtTransform Feb 05 '20 at 14:51

2 Answers2

2

Use JsonProperty attribute to specify the property name:

using Newtonsoft.Json;
// ...

[JsonProperty(PropertyName = "category")]
public string Category{ get; set; }
Krishna Varma
  • 4,238
  • 2
  • 10
  • 25
0

If you are using newer versions of Newtonsoft's serializer it will handle most simple things for you, like this. More complex ones can be handled with an attribute.

public class Category
{
    [JsonProperty(PropertyName = "category")]
    public string DifferentNameCompletely{ get; set; }
}
Jason Wadsworth
  • 8,059
  • 19
  • 32