JSON.NET会忽略数据源中的零值

时间:2019-06-26 12:52:27

标签: c# json.net json-deserialization

我正在使用... <head> <title>Alternative Style Sheet Example for MDC</title> <link href="./default.css" rel="stylesheet" type="text/css" title="Default Style"> <link href="./simple.css" rel="alternate stylesheet" type="text/css" title="Simple"> <link href="./insane.css" rel="alternate stylesheet" type="text/css" title="Insane"> </head> ... 序列化对象值,其中任何数据中的JSON.NET值都会被忽略。

zero

此处class Program { public class DataObject { public double resistance; public double reactance; }; public List<DataObject> data2 = new List<DataObject> { new DataObject { resistance= 5, reactance= -1 }, new DataObject { resistance= 0, reactance= -0.5 }, }; static void Main(string[] args) { var jsonResponse = new SerializationClass() { Points = new List<DataObject>() { new DataObject() { resistance= 5, reactance= -1 }, new DataObject() { resistance= 0, reactance= -0.5 } } }; var items = JsonConvert.SerializeObject(jsonResponse, Formatting.Indented, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore, DateFormatHandling = DateFormatHandling.IsoDateFormat }); Console.WriteLine(items); } } public class SerializationClass { [DefaultValue(null)] [JsonProperty("points")] public object Points { get { return this._points; } set { if (this._points != value) this._points = value; } } protected object _points { get; set; } } 将被忽略,并将以下内容作为resistance= 0值。

JSON

在删除{ "points": [ { "resistance": 5.0, "reactance": -1.0 }, { "reactance": -0.5 } ] } 的同时,将以下内容取为DefaultValueHandling

JSON

在这种情况下,必须删除{ "points": [ { "resistance": 5.0, "reactance": -1.0 }, { "resistance": 0.0, "reactance": -0.5 } ], "animationDuration": "2000ms" } 中未更改的属性。 (例如DefaultValue)。

我们可以指定animationDuration来解决最少的属性。但是,在我的情况下,有[JsonProperty("animationDuration", DefaultValueHandling = DefaultValueHandling.Ignore)]个具有大量属性的类。

是否有其他选择可以克服这种情况??

0 个答案:

没有答案