首页 > 代码库 > list转json和json转list
list转json和json转list
///json转list
public static List<TaskActionParameterElement> JsonToList<TaskActionParameterElement>(string JsonStr)
{
JavaScriptSerializer Serializer = new JavaScriptSerializer();
List<TaskActionParameterElement> objs = Serializer.Deserialize<List<TaskActionParameterElement>>(JsonStr);
return objs;
}
/// <summary>
/// list转json
/// </summary>
public static string ListToJson<TaskActionParameterElement>(TaskActionParameterElement data)
{
try
{
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(data.GetType());
using (MemoryStream ms = new MemoryStream())
{
serializer.WriteObject(ms, data);
return Encoding.UTF8.GetString(ms.ToArray());
}
}
catch
{
return null;
}
}
list转json和json转list