NewtonSoft.json 序列化对象中的datatime格式指定问题

[软件开发] 季雨林 2018/9/13 17:38:08

        在写app接口的时候,直接将数据库查询出的list或者 model 序列化为json输出非常方便。但是如果返回结果中有datatime类型,就有些小麻烦了。app端编写代码解析“2018-08-31T14:10:28.887”非常不方便。

默认格式: “2018-08-31T14:10:28.887”

自定义的格式:“2018-08-31 14:10:28”


        这个时候就用上了这段代码:

Model m = new Model();//Model为数据库查询的实体对象结果

IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
string json = JsonConvert.SerializeObject( m , Newtonsoft.Json.Formatting.Indented, timeFormat);

        


原文地址: https://www.opengps.cn/Blog/View.aspx?id=174 文章的更新编辑依此链接为准。欢迎关注源站原创文章!

评论

暂无评论!

发表评论:

用于接收作者回复信息
点击更换验证码 - openGPS提示