gpt4 book ai didi

c# - System.Text.JSON 不会反序列化 Newtonsoft 所做的事情

转载 作者:行者123 更新时间:2023-12-03 16:29:00 25 4
gpt4 key购买 nike

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

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

类(class):

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
牛顿软件 12.0.3

enter image description here

最佳答案

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

using System.Text.Json;

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

关于c# - System.Text.JSON 不会反序列化 Newtonsoft 所做的事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58879190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com