JSON不会反序列化Newtonsoft所做的事情

von4xj4u  于 2023-02-20  发布在  其他
关注(0)|答案(5)|浏览(176)

我有一个json,新的System.Text.Json.JsonSerializer.Deserialize<T>(json_data)将其序列化为具有正确元素数的List<T>,但其中的对象的值全部为null或0
正确填写了相同的json和Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json_data)
类别:

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}]

任何帮助?控制台应用程序项目. net核心3. 0. 0 newtonsoft 12. 0. 3

g6baxovj

g6baxovj1#

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

using System.Text.Json;

JsonSerializer.Deserialize<T>(json_data, new JsonSerializerOptions 
{
    PropertyNameCaseInsensitive = true
});
fd3cxomn

fd3cxomn2#

对于全局设置,在Program.cs/Startup.cs中设置

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

a1o7rhls3#

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

hrysbysz

hrysbysz4#

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);
}
zsbz8rwp

zsbz8rwp5#

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

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

https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-customize-properties#customize-individual-property-names
本文描述了序列化器的行为:
https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-core-3-1#serialization-behavior

相关问题