zoukankan      html  css  js  c++  java
  • c#,Model 实体转json,字符串转json

      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
        }
  • 相关阅读:
    Hive安装
    hbase安装
    Spring boot 出现的时间
    RESTful Web API 实践
    Java的进阶之道
    Spring boot 注解简单备忘
    使用 xshell 登录 Windows 的 linux 子系统
    Nginx 实用配置
    跟着大彬读源码
    跟着大彬读源码
  • 原文地址:https://www.cnblogs.com/SeNaiTes/p/9006672.html
Copyright © 2011-2022 走看看