是否可以?

我有一个这样的课:

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

最佳答案

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

using Newtonsoft.Json.Serialization;

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

关于c# - 如何用JavaScriptSerializer强制驼峰式?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17700213/

10-14 16:07
查看更多