Json格式的数据怎么转换成List
若以下回答无法解决问题,邀请你更新回答
展开全部
/// <summary>
/// Json转换成实体类,返回对象
/// </summary>
/// <typeparam name="T">反序列化类型</typeparam>
/// <param name="jsonString">反序列化字符串</param>
/// <returns>反序列化后的值</returns>
public static T JsonToModel<T>(string jsonString)
{
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
{
try
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
T returnOjbect = (T)serializer.ReadObject(ms);
return returnOjbect;
}
catch (Exception ex)
{
throw ex;
}
finally
{
ms.Close();
ms.Dispose();
}
}
}
/// <summary>
/// Json转换成List集合,返回对象List
/// </summary>
/// <typeparam name="T">反序列化类型</typeparam>
/// <param name="jsonString">反序列化字符串</param>
/// <returns>反序列化后的值</returns>
public static List<T> JsonToList<T>(string jsonString)
{
return JsonToModel<List<T>>(jsonString);
}
/// Json转换成实体类,返回对象
/// </summary>
/// <typeparam name="T">反序列化类型</typeparam>
/// <param name="jsonString">反序列化字符串</param>
/// <returns>反序列化后的值</returns>
public static T JsonToModel<T>(string jsonString)
{
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
{
try
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
T returnOjbect = (T)serializer.ReadObject(ms);
return returnOjbect;
}
catch (Exception ex)
{
throw ex;
}
finally
{
ms.Close();
ms.Dispose();
}
}
}
/// <summary>
/// Json转换成List集合,返回对象List
/// </summary>
/// <typeparam name="T">反序列化类型</typeparam>
/// <param name="jsonString">反序列化字符串</param>
/// <returns>反序列化后的值</returns>
public static List<T> JsonToList<T>(string jsonString)
{
return JsonToModel<List<T>>(jsonString);
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询