日期:2014-05-17 浏览次数:21005 次
public static class jsonToCsharp
{
public static List<T> JSONStringToList<T>(this string JsonStr)
{
JavaScriptSerializer Serializer = new JavaScriptSerializer();
List<T> objs = Serializer.Deserialize<List<T>>(JsonStr);
return objs;
}
public static T Deserialize<T>(string json)
{
T obj = Activator.CreateInstance<T>();
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
return (T)serializer.ReadObject(ms);
}
}
}
using jsonToCsharp;
......
......
string JsonStr = "[{Name:'苹果',Price:5.5},{Name:'橘子',Price:2.5},{Name:'柿子',Price:16}]";
List<Product> products = new List<Product>();
products = jsonToCsharp.JSONStringToList<Product>(JsonStr);