2012-09-19 22 views
3

我有一个操作在我的ASP.Net MVC4应用程序中返回JsonResult。我将Data属性设置为预定义类的数组。我的问题是我想序列化不同的属性名称。无论使用什么属性,该对象都会使用预定义的属性名称进行序列化。我试过没有结果如下:使用JsonResult时定义JSON字段名称

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

我知道“iTotalRecords”似乎很傻,但这个动作是用于支持jQuery插件,预计“iTotalRecords”,而不是“总记录”。当然,我想使用在我的代码隐藏中有意义的名称。

什么序列化器用于分析JsonResult?有什么我可以做或我必须重新考虑返回JsonResult作为一个行动结果?

+1

你见过[这个线程](http://stackoverflow.com/questions/1302946/asp-net-mvc-controlling-serialization-of-property-names-with-jsonresult)? –

回答

4

用什么序列化器来分析JsonResult?

JavaScriptSerializer

有什么我可以做或我必须重新考虑返回JsonResult作为一个行动结果?

两种可能性浮现在脑海中:

  • 定义视图模型,然后你的域模型映射到视图模型
  • 编写使用Json.NET或DataContractJsonSerializer并允许自定义操作结果您可以控制序列化属性的名称。 following question说明了这一点。
3

感谢您的建议。我继续创建了使用Json.Net一个ActionResult:

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)); 
    } 
} 

仅供参考,它看起来像Json.Net尊重都[数据成员]和[JsonProperty],而[JsonProperty]将王牌[数据成员]如果有差别。

+0

我喜欢这个!您应该添加一个例子使用'公共虚拟的ActionResult GetByCatalogId(GUID采用catalogId){VAR O =新的MyObject();返回新的JsonNetActionResult(o); }' – styfle

1

我已经找到了解决方案的一部分here和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(); 
     } 
    } 

所以,在我的控制,我可以做到这一点

 return new JsonNetResult(result); 

在我的模型,我现在可以有:

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

请注意,现在您必须将JsonPropertyAttribute设置为您要序列化的每个属性。

相关问题