14

我有一个 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 newtonsoft 12.0.3

在此处输入图像描述

4

5 回答 5

55

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

using System.Text.Json;

JsonSerializer.Deserialize<T>(json_data, new JsonSerializerOptions 
{
    PropertyNameCaseInsensitive = true
});
于 2019-11-15T14:45:26.463 回答
7

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

    services.AddControllers(options =>
    {
    }).AddJsonOptions(options =>
    {
        options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
        options.JsonSerializerOptions.Encoder = JavaScriptEncoder.Default;
    });
于 2021-02-02T23:32:01.753 回答
0
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);
}
于 2021-01-25T01:16:45.337 回答
0

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

于 2022-02-11T13:57:02.757 回答
0

您还可以将 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

于 2021-06-20T08:32:27.090 回答