6

Is there an automatic way in Json.NET to automatically map JSON properties from lower case underscore to pascal case no underscore. I know you can add json attributes to each property but is that the only way?

My JSON is like this:

{
    "first_name": "Roger",
    "last_name": "The Shrubber"
}

Here is the class:

public class Customer
{
    public FirstName { get; set; }
    public LastName { get; set; }
}
Greg Finzer
  • 6,714
  • 21
  • 80
  • 125
  • Ah, I voted to close but I didn't read carefully enough. Sorry about that. Please disregard. – Andrew Whitaker Jun 15 '15 at 13:36
  • 2
    If I recall correctly there is an attribute for that. Something like `JsonProperty("first_name")`. I can't remember the exact name. You'll have to check on that. – Der Kommissar Jun 15 '15 at 13:39
  • 2
    @EBrown: He mentions putting attributes on each property – Andrew Whitaker Jun 15 '15 at 13:41
  • @AndrewWhitaker My mistake. I didn't see that part. – Der Kommissar Jun 15 '15 at 13:41
  • 4
    You can do so by overriding the contract resolver, see [C# JSON.NET convention that follows Ruby property naming conventions?](http://stackoverflow.com/questions/3922874/), [How do you handle differing naming conventions when serializing C# objects to JSON?](http://stackoverflow.com/questions/8482668/), and so on. Search for "JSON.NET naming convention" for some implementations. See for example [Json.NET contract resolver that uses Ruby-style lowercase with underscore naming conventions](https://gist.github.com/crallen/9238178) on GitHub. – CodeCaster Jun 15 '15 at 13:44

0 Answers0