C#如何生成JSON字符串
若以下回答无法解决问题,邀请你更新回答
1个回答
展开全部
/// <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);
}
}
/// 单个对象转为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);
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询