如何使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
相关产品推荐
相关产品推荐

