How to force camelcase with JavaScriptSerializer?

Is it possible?

I have a class like this:

public class ABC { [Key] [ScriptIgnore] public int Id { get; set; } public string Name { get; set; } public string AnotherField { get; set; } [ScriptIgnore] public virtual User User { get; set; } } 

But I would like to serialize as follows { "name":"foo", "anotherField":"bar" } instead of { "name":"foo", "anotherField":"bar" } .

This is how I use:

 return Request.CreateResponse(HttpStatusCode.OK, new JavaScriptSerializer().Serialize(obj)); 
+7
c # asp.net-mvc asp.net-mvc-4
source share
1 answer

You can use the camel hull regenerator from Newtonsoft.Json.Serialization; You global.asax you register it when you start the application

 using Newtonsoft.Json.Serialization; protected void Application_Start() { config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); } 
-4
source share

All Articles