【发布时间】:2020-05-13 02:11:06
【问题描述】:
此示例代码
DateTime date = new DateTime(2020, 01, 27);
string str = Newtonsoft.Json.JsonConvert.SerializeObject(date, Newtonsoft.Json.Formatting.None,
new Newtonsoft.Json.JsonSerializerSettings() {
DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat });
返回 str = "\"2020-01-27T00:00:00\"" - 因此字符串的内容用引号括起来(这里像调试器一样以转义形式编写)。
但我只想要 ISO 格式的日期和时间。为什么会有引号?我怎样才能避免它们而不删除它们?
我已经查找了所有序列化程序选项,但没有找到对我有帮助的选项...
【问题讨论】:
-
因为 Json.NET 是 JSON 序列化器,但 JSON has no primitive format for dates & times 所以将
DateTime序列化为 JSON 字符串原语,根据 JSON standard 的要求引用。 -
引号是为了满足字符串的json syntax,因为没有像整数那样的全局日期时间对象。
-
你必须格式化日期时间。 c-sharpcorner.com/blogs/…
-
这些 cmets 是否回答了您的问题,或者您还需要帮助吗?你想解决什么问题? Given a DateTime object, how do I get an ISO 8601 date in string format??
-
this answer 到 Given a DateTime object, how do I get an ISO 8601 date in string format? 似乎显示了您想要的内容,请参阅dotnetfiddle.net/AMKLKo。或者你可以在某处添加一个 const 字符串:
public class Constants { public const string IsoDateTimeFormat = "yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss.fffffff"; }.
标签: c# json serialization json.net