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
        }
  • 相关阅读:
    Mockito一个方法的实例
    LIst与ArrayList区别
    mockito入门学习
    eclipse中调整字体大小和改变背景颜色
    常用sql语句
    eclipse导入代码和重新编译
    windows下登录linux的常用工具SecureCRT和常用命令
    junit4
    接口测试
    java环境
  • 原文地址:https://www.cnblogs.com/SeNaiTes/p/9006672.html
Copyright © 2011-2022 走看看