C# DateTime转Json汇总
DateTime转换成json的时候容易出现不想要的格式,在网上搜索了相关的解决方法copy如下:
参考http://www.newtonsoft.com/json/help/html/DatesInJSON.htm
public class LogEntry
{
public string Details { get; set; }
public DateTime LogDate { get; set; }
}
public void WriteJsonDates()
{
LogEntry entry = new LogEntry
{
LogDate = new DateTime(2009, 2, 15, 0, 0, 0, DateTimeKind.Utc),
Details = "Application started."
};
// default as of Json.NET 4.5
string isoJson = JsonConvert.SerializeObject(entry);
// {"Details":"Application started.","LogDate":"2009-02-15T00:00:00Z"}
JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings
{
DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
};
string microsoftJson = JsonConvert.SerializeObject(entry, microsoftDateFormatSettings);
// {"Details":"Application started.","LogDate":"\/Date(1234656000000)\/"}
string javascriptJson = JsonConvert.SerializeObject(entry, new JavaScriptDateTimeConverter());
// {"Details":"Application started.","LogDate":new Date(1234656000000)}
}
C# DateTime转Json汇总的更多相关文章
- C# DATETIME格式转换汇总 根据日期获取星期
原文:C# DATETIME格式转换汇总 根据日期获取星期 C# DateTime.Now.Year --2019(年) DateTime.Now.Month --9(月) DateTime.Now. ...
- Python datetime 转 JSON
Python datetime 转 JSON Python 中将 datetime 转换为 JSON 类型,在使用 Django 时遇到的问题. 环境: Python2.7 代码: import js ...
- How to overcome “datetime.datetime not JSON serializable” in python?
json.dumps(datetime.now) 意思是datetime.now不可json序列化,解决办法是转化成str或者加一个参数 cls=xxx 详细见: http://stackoverfl ...
- 6 - 常用模块(os,sys,time&datetime,random,json&picle,shelve,hashlib)
导入模块 想使用 Python 源文件,只需在另一个源文件里执行 import 语句 import module1[, module2[,... moduleN] from语句让你从模块中导入一个指定 ...
- 内置模块:time, datetime, random, json, pickle, os, sys, hashlib, collections, re
1.time模块 import time time.time() # 时间戳 浮点数 time.sleep() # 睡眠 time.gmtime()/time.localtime() #结构化时间 数 ...
- 模块之 time datetime random json pickle os sys hashlib collections
目录 1. time模块 1.1表示时间的几种方式: 1.2格式化字符串的时间格式 1.3不同格式时间的转换 2.datetim模块 3.random模块 4. json模块 4.1dumps.loa ...
- Django开发之Datetime类型JSON序列化时报错
前提回顾 在进行django开发view视图时,如果数据库字段是 datetime类型,在JSON序列化返回时,会出现异常 异常现象 TypeError: Object of type datetim ...
- C# DATETIME格式转换汇总 根据日期过期星期
C# DateTime.Now.Year --2019(年) DateTime.Now.Month --9(月) DateTime.Now.Day --19(日) DateTime.Now.Hou ...
- 关于在Django中Json无法序列化datetime的解决办法
我们在网页设计时经常会在前端和后台进行交互,前端回传的方法可以时redirect一个地址加上显式的参数,第二个办法就是使用Ajax结构.那么在传到view函数中进行处理后是需要通过Json格式进行返回 ...
随机推荐
- 普通B/S架构模式同步请求与AJAX异步请求区别(个人理解)
在上次面试的时候有被问到过AJAX同步与异步之间的概念问题,之前没有涉及到异步与同步的知识,所以特意脑补了一下,不是很全面... 同步请求流程:提交请求(POST/GET表单相似的提交操作)---服务 ...
- mysql分组函数
组函数针对的是指定字段的非空值.注意:where子句中不能出现组函数!!! avg() 平均值(只能针对数值型 ) max() 最大值(不限制类型) min() 最小值(不限制类型) ...
- Linux下的文件与目录操作 BY 四喜三顺
文件操作权限: chmod 三个八进制数字 文件名 其中:三个八进制数字,第一个代表本用户的权限,第二个代表同组的权限,第三个代表其他用户的权限4代表可读2代表可写1代表可执行例如:chmod 7 ...
- js解决IE不支持数组的indexOf()方法
if (!Array.indexOf) { Array.indexOf = function (obj) { ...
- texy
网络组件 WebView注册代码 <uses-permission android:name ="android.permission.INTERNET" /> 解释网 ...
- asp.net忘记密码功能
//调用接口 post public string GetResponseByPost(string mobile, string messcode, string values, string ut ...
- oracle批量修改多个表的数据
方法一 写PL/SQL,开cursor declare l_varID varchar2(20); l_varSubName varchar2(30); cursor mycur is sele ...
- Spring Boot+Cloud RestTemplate 调用IP或域名
在SpringBoot+Cloud的项目中,我们使用了自动配置的OAuth2RestTemplate,RestTemplate,但是在使用这些restTemplate的时候,url必须是服务的名称,如 ...
- nginx相关的一些记录
http redirect to https: if ($http_cf_visitor ~ '"scheme":"http"'){ rewrite ^/(.* ...
- Tomcat回收连接
最近公司一个JDK1.4的老项目升级了JDK1.6后BUG不断,最可恶的连接池被占满. 因为是使用tomcat的连接池所以在config下中添加 <Resource name="jdb ...