【问题标题】:How to force camelcase with JavaScriptSerializer?如何使用 JavaScriptSerializer 强制骆驼大小写?
【发布时间】:2013-07-16 00:44:54
【问题描述】:

有可能吗?

我有这样的课:

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

但我想像这样序列化{ "name":"foo", "anotherField":"bar" } 而不是 { "Name":"foo", "AnotherField":"bar" }.

这就是我的使用方式:

return Request.CreateResponse(HttpStatusCode.OK, new JavaScriptSerializer().Serialize(obj));

【问题讨论】:

标签: c# asp.net-mvc asp.net-mvc-4


【解决方案1】:

您可以使用来自 Newtonsoft.Json.Serialization 的驼峰式案例解析器; 在您的 global.asax 上,您在应用程序启动时注册它

using Newtonsoft.Json.Serialization;

protected void Application_Start()
{
     config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}

【讨论】:

  • 很遗憾,此代码不适用于 JavaScriptSerializer。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-12
  • 2020-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-23
  • 1970-01-01
相关资源
最近更新 更多