在 Json.NET中停用日期時間反序列化
考慮以下場景:
<br>使用Newtonsoft.Json;<br>使用Newtonsoft.Json.Linq; <p>class Program<br>{</p><pre class="brush:php;toolbar:false">static void Main(string[] args) { // Convert an object to a JObject, specifying DateParseHandling.None string s = "2012-08-08T01:54:45.3042880+00:00"; JObject j1 = JObject.FromObject(new { time = s }, new JsonSerializer { DateParseHandling = DateParseHandling.None }); // Convert the JObject back to a string string j1String = j1.ToString(); // Parse the string back into a JObject JObject j2 = JObject.Parse(j1String); // Check the type and value of the "time" property in j2 object o2 = j2["time"]; if (o2 is DateTime) { // Date deserialization was enabled: "time" is a DateTime } else { // Date deserialization was disabled: "time" is a raw string } }
}
預設情況下,Json.NET 會反序列化JSON 字串中的日期轉換為 DateTime 物件。但是,在某些情況下,您可能想要停用此行為並保留原始日期字串。為此,您可以使用以下選項:
透過停用日期反序列化,您可以保持 JSON 中日期字串的原始格式資料。
以上是如何停用 Json.NET 中的日期時間反序列化?的詳細內容。更多資訊請關注PHP中文網其他相關文章!