C# JSON帮助类(可互转)

时间:2022-11-12 17:03:40
 public class JsonHelper
{ public JsonHelper()
{
//
// TODO: Add constructor logic here
//
}
/// <summary>
/// 把对象序列化 JSON 字符串
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="obj">对象实体</param>
/// <returns>JSON字符串</returns>
public static string GetJson<T>(T obj)
{
//记住 添加引用 System.ServiceModel.Web
/**
* 如果不添加上面的引用,System.Runtime.Serialization.Json; Json是出不来的哦
* */
DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(T));
using (MemoryStream ms = new MemoryStream())
{
json.WriteObject(ms, obj);
string szJson = Encoding.UTF8.GetString(ms.ToArray());
return szJson;
}
}
/// <summary>
/// 把JSON字符串还原为对象
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="szJson">JSON字符串</param>
/// <returns>对象实体</returns>
public static T ParseFormJson<T>(string szJson)
{
T obj = Activator.CreateInstance<T>();
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szJson)))
{
DataContractJsonSerializer dcj = new DataContractJsonSerializer(typeof(T));
return (T)dcj.ReadObject(ms);
}
} }
//因为在webservice中是不能接收集合的 所以用字符串接收选择了json.

//调用:List<BIZ.T_ExEntryDetailModel> ExdetailModel ExdetailModel = UPED.JsonHelper.ParseFormJson<List<BIZ.T_ExEntryDetailModel>>(jsonStr);   //解析JSON串为类

  //实体类转换JSON串为类

List<Models.ExEntryDetailModel> detailModel =servers.GetExEntryDetailModel(......);

string jsonStr = UPED.JsonHelper.GetJson<List<Models.ExEntryDetailModel>>(detailModel);