JSONHelper
程序员文章站
2022-04-28 13:09:03
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.IO;using System.Text;using System.Web.Script. ......
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.io;
using system.text;
using system.web.script.serialization;
[serializable]
public static class jsonhelper
{
/// <summary>
/// 对象转json
/// </summary>
/// <param name="obj">对象</param>
/// <returns>json格式的字符串</returns>
public static string objecttojson(object obj)
{
javascriptserializer jss = new javascriptserializer();
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 jsontoobject<t>(string jsontext)
{
javascriptserializer jss = new javascriptserializer();
try
{
return jss.deserialize<t>(jsontext);
}
catch{}
return default(t);
}
public static string objecttojsondatetime(object obj)
{
javascriptserializer jss = new javascriptserializer();
jss.registerconverters(new javascriptconverter[] { 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.script.serialization;
public class datetimeconverter : javascriptconverter
{
public override object deserialize(idictionary<string, object> dictionary, type type, javascriptserializer serializer)
{
return new javascriptserializer().converttotype(dictionary, type);
}
public override idictionary<string, object> serialize(object obj, javascriptserializer 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, object>
{
public customstring(string str) : base(str, urikind.relative)
{
}
void idictionary<string, object>.add(string key, object value)
{
throw new notimplementedexception();
}
bool idictionary<string, object>.containskey(string key)
{
throw new notimplementedexception();
}
icollection<string> idictionary<string, object>.keys
{
get
{
throw new notimplementedexception();
}
}
bool idictionary<string, object>.remove(string key)
{
throw new notimplementedexception();
}
bool idictionary<string, object>.trygetvalue(string key, out object value)
{
throw new notimplementedexception();
}
icollection<object> idictionary<string, object>.values
{
get
{
throw new notimplementedexception();
}
}
object idictionary<string, object>.this[string key]
{
get
{
throw new notimplementedexception();
}
set
{
throw new notimplementedexception();
}
}
void icollection<keyvaluepair<string, object>>.add(keyvaluepair<string, object> item)
{
throw new notimplementedexception();
}
void icollection<keyvaluepair<string, object>>.clear()
{
throw new notimplementedexception();
}
bool icollection<keyvaluepair<string, object>>.contains(keyvaluepair<string, object> item)
{
throw new notimplementedexception();
}
void icollection<keyvaluepair<string, object>>.copyto(keyvaluepair<string, object>[] array, int arrayindex)
{
throw new notimplementedexception();
}
int icollection<keyvaluepair<string, object>>.count
{
get
{
throw new notimplementedexception();
}
}
bool icollection<keyvaluepair<string, object>>.isreadonly
{
get
{
throw new notimplementedexception();
}
}
bool icollection<keyvaluepair<string, object>>.remove(keyvaluepair<string, object> item)
{
throw new notimplementedexception();
}
ienumerator<keyvaluepair<string, object>> ienumerable<keyvaluepair<string, object>>.getenumerator()
{
throw new notimplementedexception();
}
ienumerator ienumerable.getenumerator()
{
throw new notimplementedexception();
}
}
}