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);
}

posted @ 2015-12-30 15:00  清风小筑  阅读(794)  评论(0编辑  收藏  举报