You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使Utf8Json反序列化器支持大小写不敏感(无需DataMember特性)

Utf8Json 实现大小写不敏感反序列化

你可以通过配置JsonSerializerOptions并开启PropertyNameCaseInsensitive属性,实现大小写不敏感的反序列化,无需使用[DataMember(Name ="...")]特性。

修改后的代码如下:

class Program
{
    static void Main(string[] args)
    {
        string json = "{\"testprop\":123,\"name\":\"TestObject\"}";
        var options = new JsonSerializerOptions
        {
            PropertyNameCaseInsensitive = true
        };
        var obj = JsonSerializer.Deserialize<Temp>(json, options);
        Console.ReadKey();
    }
}

public class Temp
{
    public int TestProp { get; set; }
    public string Name { get; set; }
}

开启该配置后,Utf8Json反序列化时会忽略JSON键与类属性的大小写差异,自动完成值的匹配填充。

内容的提问来源于stack exchange,提问作者Danial Ahmed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 08:45:31