你的位置:首页 > ASP.net教程

[ASP.net教程]Json序列化为对象方法


 /// <summary>
/// json 序列化为对象
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="jsonString">Json对象</param>
/// <returns></returns>
public static T JsonDeserialize<T>(string jsonString)
{
if (jsonString==""||jsonString==null)
{
return default(T);
}
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
T obj = (T)ser.ReadObject(ms);
return obj;
}

实用例子

context.Response.ContentType = "text/plain";
string updated = context.Request["updated"];
List<ShowCCK_DHModle> modelList = null;
if (updated != "")
{
modelList = JsonDeserialize<List<ShowCCK_DHModle>>(updated);
}