8

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 like this { "name":"foo", "anotherField":"bar" } instead of { "Name":"foo", "AnotherField":"bar" }.

This is how I use:

return Request.CreateResponse(HttpStatusCode.OK, new JavaScriptSerializer().Serialize(obj));
Fabricio
  • 7,705
  • 9
  • 52
  • 87
  • 1
    Should help: http://stackoverflow.com/questions/4671044/deserializing-json-responses-which-contain-attributes-that-conflict-with-keyword. And yes, it obviously means you have to leave the inbuilt serializer. – Lews Therin Jul 17 '13 at 12:58
  • If you are using JSON.NET it has a `CamelCasePropertyNamesContractResolver` see http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization for an example with Web.Api – Daniel Little Jul 17 '13 at 13:08
  • This may be a duplicate of: http://stackoverflow.com/questions/15040838/mvc-jsonresult-camelcase-serialization – Daniel Little Jul 17 '13 at 13:09

1 Answers1

-6

You can use a camel case resolver from Newtonsoft.Json.Serialization; On you global.asax you register it on application start

using Newtonsoft.Json.Serialization;

protected void Application_Start()
{
     config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}
nozari
  • 504
  • 4
  • 8