【问题标题】:System.Text.JSON doesn't deserialize what Newtonsoft doesSystem.Text.JSON 不会反序列化 Newtonsoft 所做的事情
【发布时间】:2019-11-15 14:36:03
【问题描述】:

我有一个 json,新的 System.Text.Json.JsonSerializer.Deserialize<T>(json_data) 序列化为 List<T>,具有正确的元素数量,但是里面的对象的所有值都为 null 或 0

Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json_data) 相同的json 已正确填充。

类:

public class SensorValue
    {
        public string StationCode { get; set; }
        public string SensorCode { get; set; }
        public string SensorNameIt { get; set; }
        public string SensorNameDe { get; set; }
        public string SensorNameLd { get; set; }
        public string SensorUnitMeasure { get; set; }
        public DateTime SamplingDateTime { get; set; }
        public Decimal SamplingValue { get; set; }
    }

JSON

[{"stationCode":"89190MS","sensorCode":"LT","sensorNameIt":"Temperatura dell´aria","sensorNameDe":"Lufttemperatur","sensorNameLd":"Temperatura dl’aria","sensorUnitMeasure":"°C","samplingDateTime":"2019-11-15T15:10:00","samplingValue":0.3},{"stationCode":"89190MS","sensorCode":"N","sensorNameIt":"Precipitazioni","sensorNameDe":"Niederschlag","sensorNameLd":"plueia","sensorUnitMeasure":"mm","samplingDateTime":"2019-11-15T15:10:00","samplingValue":0.4},{"stationCode":"89190MS","sensorCode":"WR","sensorNameIt":"Direzione del vento","sensorNameDe":"Windrichtung","sensorNameLd":"Direzion dl vënt","sensorUnitMeasure":"° ","samplingDateTime":"2019-11-15T15:10:00","samplingValue":165.7},{"stationCode":"89190MS","sensorCode":"WG","sensorNameIt":"Velocità del vento","sensorNameDe":"Windgeschwindigkeit","sensorNameLd":"Slune dl vënt","sensorUnitMeasure":"m/s","samplingDateTime":"2019-11-15T15:10:00","samplingValue":0.7},{"stationCode":"89190MS","sensorCode":"WG.BOE","sensorNameIt":"Velocitá raffica","sensorNameDe":"Windgeschwindigkeit Böe","sensorNameLd":"Slune dl vënt","sensorUnitMeasure":"m/s","samplingDateTime":"2019-11-15T15:10:00","samplingValue":1.3},{"stationCode":"89190MS","sensorCode":"LF","sensorNameIt":"Umidità relativa","sensorNameDe":"relative Luftfeuchte","sensorNameLd":"Tume relatif","sensorUnitMeasure":"%","samplingDateTime":"2019-11-15T15:10:00","samplingValue":100.0},{"stationCode":"89190MS","sensorCode":"LD.RED","sensorNameIt":"Pressione atmosferica","sensorNameDe":"Luftdruck","sensorNameLd":"Druch dl’aria","sensorUnitMeasure":"hPa","samplingDateTime":"2019-11-15T15:10:00","samplingValue":1006.9},{"stationCode":"89190MS","sensorCode":"GS","sensorNameIt":"Radiazione globale ","sensorNameDe":"Globalstrahlung","sensorNameLd":"Nraiazion globala ","sensorUnitMeasure":"W/m²","samplingDateTime":"2019-11-15T15:10:00","samplingValue":3.8},{"stationCode":"89190MS","sensorCode":"SD","sensorNameIt":"Durata soleggiamento","sensorNameDe":"Sonnenscheindauer","sensorNameLd":"Dureda dl surëdl","sensorUnitMeasure":"s","samplingDateTime":"2019-11-15T15:10:00","samplingValue":0.0}]

有什么帮助吗? consoleapp 项目 .net 核心 3.0.0 牛顿软件 12.0.3

【问题讨论】:

    标签: c# json system.text.json


    【解决方案1】:

    System.Text.Json 解串器的默认行为是将属性匹配为区分大小写。您需要传递选项告诉它不区分大小写:

    using System.Text.Json;
    
    JsonSerializer.Deserialize<T>(json_data, new JsonSerializerOptions 
    {
        PropertyNameCaseInsensitive = true
    });
    

    【讨论】:

    【解决方案2】:

    对于全局设置,在startup.cs中设置

        services.AddControllers(options =>
        {
        }).AddJsonOptions(options =>
        {
            options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
            options.JsonSerializerOptions.Encoder = JavaScriptEncoder.Default;
        });
    

    【讨论】:

    • 这个答案对我不起作用,因为我在我的 .net 核心 api 中使用它。我必须自己传递反序列化方法中的选项。
    【解决方案3】:
    if (response.IsSuccessStatusCode)
    {
       var json = await response.Content.ReadAsStringAsync();
    
       var options = new JsonSerializerOptions
       {
           WriteIndented = true,
           PropertyNameCaseInsensitive = true // this is the point
       };
    
       var books = JsonSerializer.Deserialize<IEnumerable<Book>>(json, options);
    }
    

    【讨论】:

      【解决方案4】:

      您还可以将 JsonPropertyName 属性应用于模型属性。

      [JsonPropertyName("yourSourceJsonKey")]
      public string YourPropertyName { get; set; }
      

      https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-customize-properties#customize-individual-property-names

      本文描述了序列化器的行为:

      https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-core-3-1#serialization-behavior

      【讨论】:

        【解决方案5】:

        我在获取默认值而不是真实值时遇到了问题。当我忘记为当前项目中的模型属性添加标识符时(set)。在使用序列化/反序列化之前,模型中的属性只需要标识符 (get)。但对我来说,两个库(System.Text.Json 和 NewtonSoft)都返回了默认值。这不是主题启动器的情况。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-07-17
          • 1970-01-01
          • 2016-11-28
          • 1970-01-01
          • 1970-01-01
          • 2020-02-20
          • 2013-06-06
          • 1970-01-01
          相关资源
          最近更新 更多