使用JsonResult时定义JSON字段名称

时间:2012-09-19 14:43:57

标签: asp.net-mvc json serialization

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

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

我知道“iTotalRecords”似乎很愚蠢,但这个动作是为了支持一个期望“iTotalRecords”而不是“TotalRecords”的jQuery插件。当然,我想在我的代码隐藏中使用有意义的名称。

使用什么序列化程序来解析JsonResult?有什么我可以做或者我必须重新考虑将JsonResult作为一个动作结果返回吗?

3 个答案:

答案 0 :(得分:4)

  

使用什么序列化程序来解析JsonResult?

JavaScriptSerializer

  

我能做什么或做什么我必须重新考虑将JsonResult作为动作结果返回?

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

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

答案 1 :(得分: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尊重[DataMember]和[JsonProperty],但如果它们不同,[JsonProperty]将胜过[DataMember]。

答案 2 :(得分: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设置为要序列化的每个属性。