首页 > 其他分享 >dotnet 使用 Newtonsoft.Json 输出枚举首字符小写

dotnet 使用 Newtonsoft.Json 输出枚举首字符小写

时间:2022-09-20 20:12:48浏览次数:66  
标签:Newtonsoft 枚举 Json StringEnumConverter 小写 dotnet Foo

本文告诉大家如何使用 Newtonsoft.Json 输出枚举首字符小写

实现方法是加上 JsonConverterAttribute 特性,传入 StringEnumConverter 转换器,再加上参数设置首字符小写

如下面代码

class F1
{
    [JsonConverter(typeof(StringEnumConverter), true)]
    public Foo Foo { get; set; }
}

enum Foo
{
    Axx,
    AxxBxx,
}

在使用 StringEnumConverter 时,可以通过构造传入参数,设置是否使用 camelCase 风格。传入参数时,可以在 JsonConverterAttribute 特性上,加上参数

标签:Newtonsoft,枚举,Json,StringEnumConverter,小写,dotnet,Foo
From: https://www.cnblogs.com/lindexi/p/16712313.html

相关文章