Home > Blockchain >  Newtonsoft json serializer how lowercase the property
Newtonsoft json serializer how lowercase the property

Time:03-22

I need display the property of json in lowercase the first word exmpple

{ name: ''} 

but instead i get

{ Name: ''} <-- Error: uppercase 

Im using: .net core 3.1 Microsoft.AspNetCore.Mvc.NewtonsoftJson 3.1.23

i write this

services.AddControllers()
            .AddNewtonsoftJson(opt =>
            {
                opt.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
                {
                    
                };
            })
            ;

not working

CodePudding user response:

You can add below code in your Startup.cs file.

public void ConfigureServices(IServiceCollection services)
{
    //...
    services.AddControllers().AddNewtonsoftJson();
    JsonConvert.DefaultSettings = () => new JsonSerializerSettings
    {
        ContractResolver = new CamelCasePropertyNamesContractResolver()
    };
    // ...
}

CodePudding user response:

I am always using this syntax

services.AddControllers()
.AddNewtonsoftJson(options =>
  options.SerializerSettings.ContractResolver =
        new CamelCasePropertyNamesContractResolver());
  • Related