newtonsoft json serialize camel-case

beginer
beginer
152 Points
46 Posts

I'm try to serialise an C# object to json by using Newtonsoft.Json.Serialization but it's returning name of properties as Pascal-case.

For an example consider the following C# class:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

By default, when returning an instance of this class from a class as JSON, it'll be serialized in the following fashion:

{
  "FirstName": "Me",
  "LastName": "Public"
}

I would like it to be serialized as:

{
  "firstName": "Me",
  "lastName": "Public"
}

I'm using C# code as:

var json = JsonConvert.SerializeObject(myPerson);
Views: 85
Total Answered: 1
Total Marked As Answer: 0
Posted On: 09-Jul-2020 00:07

Share:   fb twitter linkedin
Answers
Jak
Jak
Member
388 Points
159 Posts
         

Use json serializer setting as:

var jsonSerializerSettings = new JsonSerializerSettings
{
  ContractResolver = new CamelCasePropertyNamesContractResolver()
};
var json = JsonConvert.SerializeObject(myPerson, jsonSerializerSettings);
Posted On: 14-Jul-2020 01:08
 Log In to Chat