对象中日期类型转JSON
在转JSON的对象里面有日期类型的属性,我希望在转JSON之后日期型的不显示Date(1369624476000+0800),还是显示2013-5-5,有没有方法来实现?...
在转JSON的对象里面有日期类型的属性,我希望在转JSON之后日期型的不显示Date(1369624476000+0800),还是显示2013-5-5,有没有方法来实现?转换的方法是在网上搜索的
// 从一个对象信息生成Json串
internal static string ObjectToJson<T>(T obj)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
using (MemoryStream mStream = new MemoryStream())
{
serializer.WriteObject(mStream, obj);
byte[] dataBytes = new byte[mStream.Length];
mStream.Position = 0;
mStream.Read(dataBytes, 0, (int)mStream.Length);
return Encoding.UTF8.GetString(dataBytes);
}
} 展开
// 从一个对象信息生成Json串
internal static string ObjectToJson<T>(T obj)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
using (MemoryStream mStream = new MemoryStream())
{
serializer.WriteObject(mStream, obj);
byte[] dataBytes = new byte[mStream.Length];
mStream.Position = 0;
mStream.Read(dataBytes, 0, (int)mStream.Length);
return Encoding.UTF8.GetString(dataBytes);
}
} 展开
1个回答
展开全部
方法一:序列化后转换
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
using (MemoryStream mStream = new MemoryStream())
{
serializer.WriteObject(mStream, obj);
byte[] dataBytes = new byte[mStream.Length];
mStream.Position = 0;
mStream.Read(dataBytes, 0, (int)mStream.Length);
var jsonText=Encoding.UTF8.GetString(dataBytes);
jsonText = Regex.Replace(jsonText, @"\\/Date\((\d+)\)\\/", match =>
{
DateTime dt = new DateTime(1970, 1, 1);
dt = dt.AddMilliseconds(long.Parse(match.Groups[1].Value));
dt = dt.ToLocalTime();
return dt.ToString("yyyy-MM-dd HH:mm:ss");//这个格式自己设定一下就行了
});
return jsonText;
}
方法二:换用json.net转换
string defaultJson = JsonConvert.SerializeObject(entry);
// {"Details":"Application started.","LogDate":"\/Date(1234656000000)\/"}
string javascriptJson = JsonConvert.SerializeObject(entry, new JavaScriptDateTimeConverter());
// {"Details":"Application started.","LogDate":new Date(1234656000000)}
string isoJson = JsonConvert.SerializeObject(entry, new IsoDateTimeConverter());
// {"Details":"Application started.","LogDate":"2009-02-15T00:00:00Z"}
}
方法三:在接收端处理……
方法四:额,还没想到
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
using (MemoryStream mStream = new MemoryStream())
{
serializer.WriteObject(mStream, obj);
byte[] dataBytes = new byte[mStream.Length];
mStream.Position = 0;
mStream.Read(dataBytes, 0, (int)mStream.Length);
var jsonText=Encoding.UTF8.GetString(dataBytes);
jsonText = Regex.Replace(jsonText, @"\\/Date\((\d+)\)\\/", match =>
{
DateTime dt = new DateTime(1970, 1, 1);
dt = dt.AddMilliseconds(long.Parse(match.Groups[1].Value));
dt = dt.ToLocalTime();
return dt.ToString("yyyy-MM-dd HH:mm:ss");//这个格式自己设定一下就行了
});
return jsonText;
}
方法二:换用json.net转换
string defaultJson = JsonConvert.SerializeObject(entry);
// {"Details":"Application started.","LogDate":"\/Date(1234656000000)\/"}
string javascriptJson = JsonConvert.SerializeObject(entry, new JavaScriptDateTimeConverter());
// {"Details":"Application started.","LogDate":new Date(1234656000000)}
string isoJson = JsonConvert.SerializeObject(entry, new IsoDateTimeConverter());
// {"Details":"Application started.","LogDate":"2009-02-15T00:00:00Z"}
}
方法三:在接收端处理……
方法四:额,还没想到
追问
神人哪,我把第一个方法改了一下报异常了 正在分析“\\/Date((\d+)\)\\/\”- \ 在模式末尾非法。我现在得到的datetime的字符串是 \\/Date(1369624476000+0800)\\/\ 我就想跟 @"\\/Date\((\d+)\)\\/", 这个位置有关系,所以改了一下改为了 @\\/Date((\d+)\)\\/\,
是不是这个位置写的有问题呀?应该怎么改啊
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询