SoFunction
Updated on 2025-03-07

Two solutions to serialize C# entity objects into Json and lowercase the first letter of the field

Introduction: I recently encountered post data that connects to certain APIs in my work and needed to lowercase the field of the object.

There are two solutions:

The first one: set JsonProperty using the object's field properties to implement it (not recommended, because the properties of each field need to be manually modified)

public class UserInfo
{
  [JsonProperty("id")]
  public int Id{ set; get; }
  [JsonProperty("userName")]
  public string UserName{ set; get; }
}

The second type: use to set the formatting method (recommended)

var user = new { Name = "john", Age = 19 }; 
 var serializerSettings = new JsonSerializerSettings
      {
        // Set as camel name        ContractResolver = new CamelCasePropertyNamesContractResolver()
      };
var userStr = (user, , serializerSettings);

Summarize

The above are two solutions to serialize C# entity objects into Json and lowercase the first letter of the field. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!