先添加程序集: System.Web.Extensions(在 System.Web.Extensions.dll 中)
引用:using System.Web.Script.Serialization;
序列化
public string ScriptSerialize<T>(T t)
{
JavaScriptSerializer js = new JavaScriptSerializer();
return js.Serialize(t);
}
调用:string strJson=ScriptSerialize(users);//users是一个类
反序列化
public T ScriptDeserialize<T>(string strJson)
{
JavaScriptSerializer js = new JavaScriptSerializer();
return js.Deserialize<T>(strJson);
}
调用:Users users = ScriptDeserialize<Users>(strJson)