![](/img/trans.png)
[英]Why does Newtonsoft.Json work in example and System.Text.Json doesn't
[英]System.Text.JSON doesn't deserialize what Newtonsoft does
我有一個 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}]
有什么幫助嗎? consoleapp 項目 .net 核心 3.0.0 newtonsoft 12.0.3
System.Text.Json
反序列化程序的默認行為是將屬性匹配為區分大小寫。 您需要傳遞選項告訴它不區分大小寫:
using System.Text.Json;
JsonSerializer.Deserialize<T>(json_data, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
對於全局設置,在 startup.cs 中設置
services.AddControllers(options =>
{
}).AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
options.JsonSerializerOptions.Encoder = JavaScriptEncoder.Default;
});
我在獲取默認值而不是真實值時遇到問題。 當我忘記為當前項目中的 model 的屬性添加標識符時( set )。 在使用序列化/反序列化之前,model 中的屬性只需要標識符 (get)。但對我來說,兩個庫(System.Text.Json 和 NewtonSoft)都返回了默認值。 這不是主題啟動器的情況。
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);
}
您還可以將 JsonPropertyName 屬性應用於 model 屬性。
[JsonPropertyName("yourSourceJsonKey")]
public string YourPropertyName { get; set; }
本文描述了序列化程序的行為:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.