/// <summary>
/// Json序列化,用于发送到客户端
/// </summary>
public static string ToJsJson(this object item)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(item.GetType());
using (MemoryStream ms = new MemoryStream())
{
serializer.WriteObject(ms, item);
StringBuilder sb = new StringBuilder();
sb.Append(Encoding.UTF8.GetString(ms.ToArray()));
return sb.ToString();
}
}
/// <summary>
/// Json反序列化,用于接收客户端Json后生成对应的对象
/// </summary>
public static T FromJsonTo<T>(this string jsonString)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
T jsonObject = (T)ser.ReadObject(ms);
ms.Close();
return jsonObject;
}
js转对象为json
function aa(){
this.a="3";
this.b="4";
}
var json =stringfy(new aa());