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