MVC JsonResult camelCase序列化

这个问题在这里已经有了答案:

  • 正确的JSON序列化在MVC 4 3答案

  • Controller的Json函数只是创建JsonResults的包装器:

    protected internal JsonResult Json(object data)
    {
        return Json(data, null /* contentType */, null /* contentEncoding */, JsonRequestBehavior.DenyGet);
    }
    
    protected internal JsonResult Json(object data, string contentType)
    {
        return Json(data, contentType, null /* contentEncoding */, JsonRequestBehavior.DenyGet);
    }
    
    protected internal virtual JsonResult Json(object data, string contentType, Encoding contentEncoding)
    {
        return Json(data, contentType, contentEncoding, JsonRequestBehavior.DenyGet);
    }
    
    protected internal JsonResult Json(object data, JsonRequestBehavior behavior)
    {
        return Json(data, null /* contentType */, null /* contentEncoding */, behavior);
    }
    
    protected internal JsonResult Json(object data, string contentType, JsonRequestBehavior behavior)
    {
        return Json(data, contentType, null /* contentEncoding */, behavior);
    }
    
    protected internal virtual JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)
    {
        return new JsonResult
        {
            Data = data,
            ContentType = contentType,
            ContentEncoding = contentEncoding,
            JsonRequestBehavior = behavior
        };
    }
    

    JsonResult在内部使用JavaScriptSerializer,因此您无法控制序列化过程:

    public class JsonResult : ActionResult
    {
        public JsonResult()
        {
            JsonRequestBehavior = JsonRequestBehavior.DenyGet;
        }
    
        public Encoding ContentEncoding { get; set; }
    
        public string ContentType { get; set; }
    
        public object Data { get; set; }
    
        public JsonRequestBehavior JsonRequestBehavior { get; set; }
    
        /// <summary>
        /// When set MaxJsonLength passed to the JavaScriptSerializer.
        /// </summary>
        public int? MaxJsonLength { get; set; }
    
        /// <summary>
        /// When set RecursionLimit passed to the JavaScriptSerializer.
        /// </summary>
        public int? RecursionLimit { get; set; }
    
        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (JsonRequestBehavior == JsonRequestBehavior.DenyGet &&
                String.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
            {
                throw new InvalidOperationException(MvcResources.JsonRequest_GetNotAllowed);
            }
    
            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)
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                if (MaxJsonLength.HasValue)
                {
                    serializer.MaxJsonLength = MaxJsonLength.Value;
                }
                if (RecursionLimit.HasValue)
                {
                    serializer.RecursionLimit = RecursionLimit.Value;
                }
                response.Write(serializer.Serialize(Data));
            }
        }
    }
    

    你必须创建你自己的JsonResult并编写你自己的Json控制器函数(如果你需要/想要的话)。 您可以创建新的或覆盖现有的,取决于您。 这个链接也可能让你感兴趣。


    如果您想从符合camelcase符号的操作中返回json字符串,则必须创建JsonSerializerSettings实例并将其作为JsonConvert.SerializeObject(a,b)方法的第二个参数传递。

    public string GetSerializedCourseVms()
        {
            var courses = new[]
            {
                new CourseVm{Number = "CREA101", Name = "Care of Magical Creatures", Instructor ="Rubeus Hagrid"},
                new CourseVm{Number = "DARK502", Name = "Defence against dark arts", Instructor ="Severus Snape"},
                new CourseVm{Number = "TRAN201", Name = "Transfiguration", Instructor ="Minerva McGonal"}
            };
            var camelCaseFormatter = new JsonSerializerSettings();
            camelCaseFormatter.ContractResolver = new CamelCasePropertyNamesContractResolver();
            return JsonConvert.SerializeObject(courses, camelCaseFormatter);
        }
    
    链接地址: http://www.djcxy.com/p/48143.html

    上一篇: MVC JsonResult camelCase serialization

    下一篇: Insert HTML into view