Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

c# - System.Text.JSON doesn't deserialize what Newtonsoft does

I have a json that the new System.Text.Json.JsonSerializer.Deserialize<T>(json_data) serialize as List<T> with the correct numbers of elements, but then the objects inside have all the values null or 0

Same json with Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json_data) is correctly filled.

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/m2","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}]

Any help ? consoleapp project .net core 3.0.0 newtonsoft 12.0.3

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The default behavior of the System.Text.Json deserializer is to match properties as case sensitive. You need to pass options telling it to match case insensitive:

using System.Text.Json;

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...