相关文章推荐
私奔的山楂  ·  QtDesigner和pyqt5编程_pyq ...·  1 年前    · 
捣蛋的长颈鹿  ·  PaddlePaddle ...·  1 年前    · 
私奔的山楂  ·  NSIS + QT ...·  2 年前    · 

本文将介绍如何使用 System.Text.Json 命名空间启用不区分大小写的属性名称匹配。

不区分大小写的属性匹配

默认情况下,反序列化会查找 JSON 与目标对象属性之间区分大小写的属性名称匹配。 若要更改该行为,请将 JsonSerializerOptions.PropertyNameCaseInsensitive 设置为 true

Web 默认值 为不区分大小写。

var options = new JsonSerializerOptions PropertyNameCaseInsensitive = true var weatherForecast = JsonSerializer.Deserialize<WeatherForecast>(jsonString, options); Dim options As JsonSerializerOptions = New JsonSerializerOptions With { .PropertyNameCaseInsensitive = True Dim weatherForecast1 = JsonSerializer.Deserialize(Of WeatherForecast)(jsonString, options)

下面是具有 camel 大小写属性名称的示例 JSON。 它可以反序列化为具有帕斯卡拼写法属性名称的以下类型。

"date": "2019-08-01T00:00:00-07:00", "temperatureCelsius": 25, "summary": "Hot", public class WeatherForecast public DateTimeOffset Date { get; set; } public int TemperatureCelsius { get; set; } public string? Summary { get; set; } Public Class WeatherForecast Public Property [Date] As DateTimeOffset Public Property TemperatureCelsius As Integer Public Property Summary As String End Class
  • System.Text.Json 概述
  • 如何对 JSON 进行序列化和反序列化
  • 对 JsonSerializerOptions 实例进行实例化
  • 自定义属性名称和值
  • 允许无效的 JSON
  • 处理溢出 JSON,或者使用 JsonElement 或 JsonNode
  • 保留引用并处理循环引用
  • 反序列化为不可变类型和非公共访问器
  • 多态序列化
  • 从 Newtonsoft.Json 迁移到 System.Text.Json
  • 自定义字符编码
  • 使用 DOM、Utf8JsonReader 和 Utf8JsonWriter
  • 编写用于 JSON 序列化的自定义转换器
  • DateTime 和 DateTimeOffset 支持
  • 如何使用源生成
  • 受支持的集合类型
  • System.Text.Json API 参考
  • System.Text.Json.Serialization API 参考
  •