c#,Model 实体转json,字符串转json

mac2022-06-30  25

  public class JsonF { #region 字符串转json /// <summary> /// 字符串转json /// </summary> /// <param name="obj"></param> /// <returns></returns> public static HttpResponseMessage toJson(Object obj) { String str; if (obj is String || obj is Char) { str = obj.ToString(); } else { JavaScriptSerializer serializer = new JavaScriptSerializer(); str = serializer.Serialize(obj); } HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(str, Encoding.GetEncoding("UTF-8"), "application/json") }; return result; } #endregion #region Model 实体转json /// <summary> /// Model 实体转json /// </summary> /// <param name="Model">可以是单个实体,也可是实体集(即:ToList())</param> /// <returns></returns> public static HttpResponseMessage PostModel(object Model) { JavaScriptSerializer serializer = new JavaScriptSerializer(); string str = serializer.Serialize(Model); HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(str, Encoding.GetEncoding("UTF-8"), "application/json") }; return result; } #endregion }

 

转载于:https://www.cnblogs.com/SeNaiTes/p/9006672.html

相关资源:C# 转换实体类为Json字符串
最新回复(0)