当值遇到 'Null' 时使用 Jsonseralizer

Using Jsonseralizer when the value comes across with 'Null'

我有一个 json 字符串,我试图序列化到一个对象中,但有些值是 'null',而不是空的 null,但实际上是 null 这个词,不幸的是那些是不是字符串值。我试图在 writingnull 时添加默认忽略条件,但仅此一项似乎不起作用。错误是:

InvalidOperationException:无法获取令牌类型 'Null' 的值作为数字

我的代码:

 var settings = new JsonSerializerOptions
            {
                DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull   
            };
gdata = JsonSerializer.Deserialize<List<gasData>>(response.Content, settings);

来自 json 原始数据的示例:

"no_offset":0.0000,"no_units":"ppb","so2_sensor_serial_number":null,"so2_state":"Not Fitted","so2_prescaled":null

试试这个代码,它工作正常

var json = "{\"no_offset\":0,\"no_units\":\"ppb\",\"so2_sensor_serial_number\":null,\"so2_state\":\"Not Fitted\",\"so2_prescaled\":null}";

var data = System.Text.Json.JsonSerializer.Deserialize<Data>(json);

class

public partial class Data
{
    [JsonPropertyName("no_offset")]
    public long? NoOffset { get; set; }

    [JsonPropertyName("no_units")]
    public string NoUnits { get; set; }

    [JsonPropertyName("so2_sensor_serial_number")]
    public long? So2SensorSerialNumber { get; set; }

    [JsonPropertyName("so2_state")]
    public string So2State { get; set; }

    [JsonPropertyName("so2_prescaled")]
    public object So2Prescaled { get; set; }
}