using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Text;
using System.Web. .Serialization;
[Serializable] public static class JS er { /// <summary> /// 对象转JSON /// </summary> /// <param name=\"obj\">对象</param> /// <returns>JSON格式的字符串</returns> public static string ToJSON( obj) { Serializer jss = new Serializer(); try { return jss.Serialize(obj); } catch { } return null; } /// <summary> /// /// JSON文本转对象,泛型方法 /// </summary> /// <typeparam name=\"T\">类型</typeparam> /// <param name=\"jsonText\">JSON文本</param> /// <returns>指定类型的对象</returns> public static T JSONTo <T>(string jsonText) { Serializer jss = new Serializer(); try { return jss.Deserialize<T>(jsonText); } catch{} return default(T); } public static string ToJsonDateTime( obj) { Serializer jss = new Serializer(); jss.RegisterConverters(new Converter[] { new DateTimeConverter() }); try { return jss.Serialize(obj); } catch { } return null; } }
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web. .Serialization;
public class DateTimeConverter : Converter
{
public override Deserialize(IDictionary<string, > dictionary, Type type, Serializer serializer)
{
return new Serializer().ConvertToType(dictionary, type);
}
public override IDictionary<string, > Serialize( obj, Serializer serializer)
{
if (!(obj is DateTime))
{
return null;
}
return new CustomString(((DateTime)obj).ToString(\"yyyy-MM-dd HH:mm:ss\"));
}
public override IEnumerable<Type> SupportedTypes
{
get
{
return new[] { typeof(DateTime) };
}
}
private class CustomString : Uri, IDictionary<string, >
{
public CustomString(string str) : (str, UriKind.Relative)
{
}
void IDictionary<string, >.Add(string key, value)
{
throw new NotImplementedException();
}
bool IDictionary<string, >.ContainsKey(string key)
{
throw new NotImplementedException();
}
ICollection<string> IDictionary<string, >.Keys
{
get
{
throw new NotImplementedException();
}
}
bool IDictionary<string, >.Remove(string key)
{
throw new NotImplementedException();
}
bool IDictionary<string, >.TryGetValue(string key, out value)
{
throw new NotImplementedException();
}
ICollection< > IDictionary<string, >.Values
{
get
{
throw new NotImplementedException();
}
}
IDictionary<string, >.this[string key]
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
void ICollection<KeyValuePair<string, >>.Add(KeyValuePair<string, > item)
{
throw new NotImplementedException();
}
void ICollection<KeyValuePair<string, >>.Clear()
{
throw new NotImplementedException();
}
bool ICollection<KeyValuePair<string, >>.Contains(KeyValuePair<string, > item)
{
throw new NotImplementedException();
}
void ICollection<KeyValuePair<string, >>.CopyTo(KeyValuePair<string, >[] array, int arrayIndex)
{
throw new NotImplementedException();
}
int ICollection<KeyValuePair<string, >>.Count
{
get
{
throw new NotImplementedException();
}
}
bool ICollection<KeyValuePair<string, >>.IsReadOnly
{
get
{
throw new NotImplementedException();
}
}
bool ICollection<KeyValuePair<string, >>.Remove(KeyValuePair<string, > item)
{
throw new NotImplementedException();
}
IEnumerator<KeyValuePair<string, >> IEnumerable<KeyValuePair<string, >>.GetEnumerator()
{
throw new NotImplementedException();
}
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}
}
继续阅读与本文标签相同的文章
上一篇 :
python自动化测试(六)yaml文件管理数据
下一篇 :
如何正确并快速理解MapReduce
-
给网站添加微信扫描二维码登录功能
2026-05-19栏目: 教程
-
Sharding-Jdbc之读写分离导读
2026-05-19栏目: 教程
-
Sharding-Jdbc分库分表的导读
2026-05-19栏目: 教程
-
日志服务数据加工培训直播资料汇总: 扫平日志分析路上障碍, 实时海量日志加工实践
2026-05-19栏目: 教程
-
OCP-052考试题库汇总(46)-CUUG内部解答版
2026-05-19栏目: 教程
