zoukankan      html  css  js  c++  java
  • ASP.NET JsonResult返回日期格式及首字母大写解决

    添加一个类继承JsonResult

      public class CustomJsonResult : JsonResult
        {
            private const string _dateFormat = "yyyy-MM-dd HH:mm:ss";
    
            public CustomJsonResult()
            {
                serializerSettings = new JsonSerializerSettings
                {
                    // 设置为驼峰命名
                    ContractResolver = new CamelCasePropertyNamesContractResolver(), //首字母小写
                    DateFormatString = _dateFormat   //日期格式
    
                 };
            }
            private JsonSerializerSettings serializerSettings { get; set; }
            public override void ExecuteResult(ControllerContext context)
            {
                if (context == null)
                {
                    throw new ArgumentNullException("context");
                }
    
                HttpResponseBase response = context.HttpContext.Response;
    
                if (!String.IsNullOrEmpty(ContentType))
                {
                    response.ContentType = ContentType;
                }
                else
                {
                    response.ContentType = "application/json";
                }
                if (ContentEncoding != null)
                {
                    response.ContentEncoding = ContentEncoding;
                }
                if (Data != null)
                {
                    // Using Json.NET serializer                     
                    var sctiptSerialize = JsonSerializer.Create(serializerSettings);    
                    sctiptSerialize.Serialize(response.Output, Data);
                }
            }

    调用:

     public ActionResult Test()
            {
                Person p = new Person() { Name = "zhangsan", Age = 1, BirthDay = DateTime.Now };
                //return Json(d);
                return new CustomJsonResult {Data=p };
            }
  • 相关阅读:
    python爬虫出现的状态码
    FreeSWITCH部署与功能配置
    爬虫读取内容常见的3种方式
    python:3种爬虫的优缺点
    JSON数据解析
    FreeSWITCH与FreeSWITCH对接
    FreeSWITCH添加中文语音
    异步加载技术与逆向工程概念
    word页眉与页脚详解
    修改MyEclipse内存
  • 原文地址:https://www.cnblogs.com/Zingu/p/14711100.html
Copyright © 2011-2022 走看看