C# Json 日期时间问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11757757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:07:24  来源:igfitidea点击:

Json Datetime issue

c#asp.netjsondatetimejson.net

提问by Amol Kolekar

Possible Duplicate:
how to force netwtonsoft json serializer to serialize datetime property to string?

可能的重复:
如何强制 netwtonsoft json 序列化程序将日期时间属性序列化为字符串?

I am using Newtonsoft.Json to convert my object in to JSON file. But I am having issue with the DateTime, in my object datetime field is set as "7/30/2012 8:29:12 PM"but in JSON file. I am getting DateTimefield in this format:

我正在使用 Newtonsoft.Json 将我的对象转换为 JSON 文件。但是我遇到了DateTime, 在我的对象日期时间字段中设置为"7/30/2012 8:29:12 PM"但在 JSON 文件中的问题。我正在DateTime以这种格式获取字段:

"\/Date(1343660352227+0530)\/".

"\/Date(1343660352227+0530)\/".

I need to get the DateTimein the same format as that of the object. Is it possible? What should I do in order to get the same format?

我需要以DateTime与对象相同的格式获取。是否可以?我应该怎么做才能获得相同的格式?

采纳答案by Amol Kolekar

I got the solution as follow.

我得到了如下解决方案。

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

Here is the answer:

这是答案:

how to force netwtonsoft json serializer to serialize datetime property to string?

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