我正在使用swagger的.net core webapi项目,当我把这个请求放进swagger时:
{
"year": 2023,
"vat_amount": 2000,
"vat_period": "January",
"vat_amount_20": 2000,
"turnover_description": "Some description of turnover",
"turnover_description_10": "Turnover Description 10",
"turnover_description_20": "Turnover Description 20",
"related_party_identifier": "106723546"
}
字符串
Newtonsoft.JSON可以识别所有内容,除了 prop 与字母数字组合的地方:vat_amount_20
,turnover_description_10
,turnover_description_20
这些字段未在我的dto模型中填充:
[JsonObject(NamingStrategyType = typeof(SnakeCaseNamingStrategy))]
public class VatRecordingIndividualRequestDto
{
[JsonProperty("turnover_description")]
public string? TurnoverDescription { get; set; }
[JsonProperty("turnover_description_10")]
public string? TurnoverDescription10 { get; set; }
[JsonProperty("turnover_description_20")]
public string? TurnoverDescription20 { get; set; }
[JsonProperty("vat_amount_20")]
public double? VatAmount20 { get; set; }
[JsonProperty("vat_amount")]
public double? VatAmount { get; set; }
[JsonProperty("vat_period")]
public string? VatPeriod { get; set; }
[JsonProperty("year")]
public int Year { get; set; }
}
型
在我的program.cs中,我为SnakeCasePropertyNamingPolicy设置了JsonOptions:
builder.Services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = new SnakeCasePropertyNamingPolicy();
});
型
我也从这个代码调用这个类:
public class SnakeCasePropertyNamingPolicy : JsonNamingPolicy
{
public override string ConvertName(string name)
{
return name.ToSnakeCase();
}
}
public static class StringExtensions
{
public static string ToSnakeCase(this string str)
{
return string.Concat(str.Select((character, index) =>
index > 0 && char.IsUpper(character)
? "_" + character
: character.ToString()))
.ToLower();
}
}
型
*如何强制我的dto模型考虑json的以下特性,例如:foo_bar_1?
1条答案
按热度按时间dfuffjeb1#
你需要正确处理数字?当它发现一个数字前面有一个非数字字符时,它应该插入一个下划线。
字符串