c# - 使属性不区分大小写

标签 c# .net utf8json

编辑:我正在使用 Utf8Json不是System.Text.Json

有什么方法可以使 Utf8Json Deserializer 不区分大小写吗?目前,如果 json key-case 与 property-case 不匹配,则不会填充值。

我不想使用 [DataMember(Name ="...")]

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

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

最佳答案

你只需要传入一个JsonSerializerOptions带有 PropertyNameCaseInsensitive 的对象属性设置为真。例如:

var options = new JsonSerializerOptions
{
    PropertyNameCaseInsensitive = true
};

var obj = JsonSerializer.Deserialize<Temp>(json, options);

关于c# - 使属性不区分大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73048541/

相关文章:

c# - 如何使用linq to sql从行中选择一列

c# - 在一台计算机上打开两个透明度为="true"的wpf应用程序时,只有一个显示

c# - 如何在c#中禁用按钮颜色

c# - 如何从 IQueryable 变量中获取数据字段?

.net - 我可以确保 WCF 服务中 TCP 的互操作性吗?

c# - 绘图区中的 GTK# 鼠标事件

c# - 从字符串中提取可变数量的 token 对到一对数组

c# - 使用 Utf8Json 库序列化时排除空字段?