I have a web service with .NET 7. When I get the data from the database and send it as Jason, the first letter of the class items is sent in lowercase.
In order to explain the problem, I wrote a test web service
testClass.cs
public class testClass
{
public String Date1 { get; set; }
public String Date2 { get; set; }
public testClass(string date1, string date2)
{
Date1 = date1;
Date2 = date2;
}
}
and my api:
[HttpGet(Name = "test")]
public testClass Get()
{
return (new testClass("d1", "d2"));
}
the result is:
{
"date1": "d1",
"date2": "d2"
}
But I want the result to be as follows:
{
"Date1": "d1",
"Date2": "d2"
}
CodePudding user response:
Or just add JsonProperty:
public class testClass
{
[JsonProperty("Date1")]
public String Date1 { get; set; }
[JsonProperty("Date2")]
public String Date2 { get; set; }
public testClass(string date1, string date2)
{
Date1 = date1;
Date2 = date2;
}
}
CodePudding user response:
Default mode for JsonSerializerOptions
is camel-casing
(The first word starts with a small letter and capital letter appears at the start of the second word and at each new subsequent word that follows it).
You can get more information in this link: JsonSerializerOptions.PropertyNamingPolicy
You just need to add the following configuration to Program.cs
:
builder.Services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = null;
});