'Newtonsoft json serializer how lowercase the property
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
Solution 1:[1]
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()
};
// ...
}
Solution 2:[2]
I am always using this syntax
services.AddControllers()
.AddNewtonsoftJson(options =>
options.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver());
Solution 3:[3]
i found the problem is because im creating a dynamic object using Newtonsoft class
Newtonsoft.Json.Linq.JObject obj = new Newtonsoft.Json.Linq.JObject();
obj.Add(new Newtonsoft.Json.Linq.JProperty("Emp",item.Key.E));
obj.Add(new Newtonsoft.Json.Linq.JProperty("Dep",item.Key.D));
obj.Add(new Newtonsoft.Json.Linq.JProperty("Pu",item.Key.P));
and this json object are not convert to CamelCase and my dtos class yes
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jason |
| Solution 2 | Serge |
| Solution 3 | juan carlos peña cabrera |
