首页 > 代码库 > JSON反序列化
JSON反序列化
/// <summary>
/// JSON反序列化
/// </summary>
public static T JsonDeserialize<T>(string jsonString)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
T obj = (T)ser.ReadObject(ms);
return obj;
}
// List<shopMode> obj = util.HttpWeb.DeserializeFromJson<List<shopMode>>(r);
/// <summary>
/// JSON反序列化集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="jsonString"></param>
/// <returns></returns>
public static T DeserializeFromJson<T>(string jsonString) where T : class
{
DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
{
T obj = (T)ds.ReadObject(ms);
return obj;
}
}
JSON反序列化