Determine JSON Field Names When Using JsonResult

I have an action that returns JsonResult in my ASP.Net MVC4 application. I set the Data property to an array of predefined classes. My problem is that I want to serialize with different property names. No matter what attributes I use, the object is serialized with predefined property names. I tried the following without any results:

[DataMember(Name = "iTotalRecords")] [JsonProperty(PropertyName = "iTotalRecords")] public int TotalRecords { get; set; } 

I know that "iTotalRecords" seems silly, but this action is intended to support the jQuery plugin, which expects "iTotalRecords" and not "TotalRecords". Of course, I want to use names that make sense in my code.

What serializer is used to parse JsonResult? Is there anything I can do, or do I need to think again about returning JsonResult as the result of the action?

+3
source share
3 answers

What serializer is used to parse JsonResult?

JavaScriptSerializer

Is there anything I can do, or do I need to think again about returning JsonResult as the result of the action?

Two possibilities come to mind:

  • Define a view model, and then map your domain model to the view model.
  • write the result of a custom action that uses Json.NET or DataContractJsonSerializer and which allows you to manage the names of serialized properties. The following question illustrates this.
+4
source

Thanks for the suggestions. I went ahead and created an ActionResult that uses Json.Net:

 public class JsonNetActionResult : ActionResult { public Object Data { get; private set; } public JsonNetActionResult(Object data) { this.Data = data; } public override void ExecuteResult(ControllerContext context) { context.HttpContext.Response.ContentType = "application/json"; context.HttpContext.Response.Write(JsonConvert.SerializeObject(Data)); } } 

FYI seems to be Json.Net respecting both [DataMember] and [JsonProperty], but [JsonProperty] will surpass [DataMember] if they are different.

+3
source

I found part of the solution here and on SO

 public class JsonNetResult : ActionResult { public Encoding ContentEncoding { get; set; } public string ContentType { get; set; } public object Data { get; set; } public JsonSerializerSettings SerializerSettings { get; set; } public Formatting Formatting { get; set; } public JsonNetResult(object data, Formatting formatting) : this(data) { Formatting = formatting; } public JsonNetResult(object data):this() { Data = data; } public JsonNetResult() { Formatting = Formatting.None; SerializerSettings = new JsonSerializerSettings(); } public override void ExecuteResult(ControllerContext context) { if (context == null) throw new ArgumentNullException("context"); var response = context.HttpContext.Response; response.ContentType = !string.IsNullOrEmpty(ContentType) ? ContentType : "application/json"; if (ContentEncoding != null) response.ContentEncoding = ContentEncoding; if (Data == null) return; var writer = new JsonTextWriter(response.Output) { Formatting = Formatting }; var serializer = JsonSerializer.Create(SerializerSettings); serializer.Serialize(writer, Data); writer.Flush(); } } 

So in my controller I can do it

  return new JsonNetResult(result); 

In my model, I can now:

  [JsonProperty(PropertyName = "n")] public string Name { get; set; } 

Note that you must now set JsonPropertyAttribute for each property that you want to serialize.

+1
source

All Articles