gpt4 book ai didi

asp.net - 如何强制 netwtonsoft json 序列化程序将日期时间属性序列化为字符串?

转载 作者:行者123 更新时间:2023-12-03 07:17:47 24 4
gpt4 key购买 nike

我正在使用Newtonsoft的Json当我序列化日期时间属性时,我得到的 json 响应为:

..."CreatedOn":"\/Date(1317303882420+0500)\/",...

我希望它是简单的字符串

..."createdOn": "2011-05-05 14:03:07", ...

虽然我的类属性是 DateTime,但我如何强制将其序列化为字符串,因为我们可以添加属性来将属性名称更改为

  [JsonProperty("id")]
public int ProductID { get; set; }

是否有类似的方法来强制 DateTime 属性序列化为字符串?

最佳答案

从 James Newton-King 在 StackOverflow 上发表的帖子来看,您似乎可以做到这一点。

string isoJson = JsonConvert.SerializeObject(this, new IsoDateTimeConverter());
// {"Details":"Application started.","LogDate":"2009-02-15T00:00:00Z"}

引用答案: Parsing JSON DateTime from Newtonsoft's JSON Serializer

这里还有关于 Json.NET 和日期的文档: Serializing Dates in JSON

以下是使用 DateTimeFormat 属性自定义输出的示例:

return JsonConvert.SerializeObject(this, Formatting.None, new IsoDateTimeConverter() { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" });

关于asp.net - 如何强制 netwtonsoft json 序列化程序将日期时间属性序列化为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7670629/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com