System.Text.JSON 不反序列化 Newtonsoft 所做的
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 core 3.0.0
牛顿软件 12.0.3
System.Text.Json
反序列化器的默认行为是将属性匹配为区分大小写。您需要传递选项,告诉它不区分大小写:
using System.Text.Json;
JsonSerializer.Deserialize<T>(json_data, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
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);
}
全局设置,在startup.cs中设置
services.AddControllers(options =>
{
}).AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
options.JsonSerializerOptions.Encoder = JavaScriptEncoder.Default;
});
您还可以将 JsonPropertyName 属性应用于模型属性。
[JsonPropertyName("yourSourceJsonKey")]
public string YourPropertyName { get; set; }
本文描述了序列化程序的行为:
我在获取默认值而不是真实值时遇到了问题。当我忘记为当前项目中模型的属性添加标识符时(set)。在使用 serialization/deserialization 之前,模型中的属性只需要标识符 (get)。但对我来说,两个库(System.Text.Json 和 NewtonSoft)都返回了默认值。这不是话题发起人的情况。
我有一个 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 core 3.0.0 牛顿软件 12.0.3
System.Text.Json
反序列化器的默认行为是将属性匹配为区分大小写。您需要传递选项,告诉它不区分大小写:
using System.Text.Json;
JsonSerializer.Deserialize<T>(json_data, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
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);
}
全局设置,在startup.cs中设置
services.AddControllers(options =>
{
}).AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
options.JsonSerializerOptions.Encoder = JavaScriptEncoder.Default;
});
您还可以将 JsonPropertyName 属性应用于模型属性。
[JsonPropertyName("yourSourceJsonKey")]
public string YourPropertyName { get; set; }
本文描述了序列化程序的行为:
我在获取默认值而不是真实值时遇到了问题。当我忘记为当前项目中模型的属性添加标识符时(set)。在使用 serialization/deserialization 之前,模型中的属性只需要标识符 (get)。但对我来说,两个库(System.Text.Json 和 NewtonSoft)都返回了默认值。这不是话题发起人的情况。