加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何使用JavaScriptSerializer强制使用camelcase?

发布时间:2020-12-15 18:31:53 所属栏目:百科 来源:网络整理
导读:可能吗? 我有一个这样的课: 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; }} 但我想像这样序
可能吗?

我有一个这样的课:

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”}而不是
{“姓名”:“foo”,“AnotherField”:“bar”}.

这就是我的用法:

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

解决方法

你可以使用Newtonsoft.Json.Serialization的camel case解析器;
在你的global.asax上,你在应用程序启动时注册它
using Newtonsoft.Json.Serialization;

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读