c#如何将字符串生成json数组
展开全部
给你,拿去用
/// <summary>
/// 单个对象转为json
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="json">数据</param>
/// <returns>json数据</returns>
public static T Json2Obj<T>(string json)
{
T obj = Activator.CreateInstance<T>();
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(json)))
{
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
return (T)serializer.ReadObject(ms);
}
}
/// <summary>
/// 单个对象转为json
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="json">数据</param>
/// <returns>json数据</returns>
public static T Json2Obj<T>(string json)
{
T obj = Activator.CreateInstance<T>();
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(json)))
{
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
return (T)serializer.ReadObject(ms);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询