Proper JSON serialization in MVC 4

前端 未结 4 2121
不思量自难忘°
不思量自难忘° 2020-11-27 02:54

I\'d like to have JSON \'properly\' serialized (camelCase), and the ability to change date formats if necessary.

For Web API it is very easy - in the Global.asax I

4条回答
  •  暖寄归人
    2020-11-27 03:36

    While Web API uses JSON.NET, MVC4 uses by default the JavaScriptSerializer, and I dont think it supports changing to Camel Case serialization. Check this: Setting the default JSON serializer in ASP.NET MVC

    My suggestion is that you create a custom JsonNetResult as described here Using JSON.NET as the default JSON serializer in ASP.NET MVC 3 - is it possible? and change the last line to:

    var serializedObject = JsonConvert.SerializeObject(
          Data,
          Formatting.Indented,
          new JsonSerializerSettings { MappingResolver = new CamelCaseMappingResolver() });
    

提交回复
热议问题