【问题标题】:Can I use CamelCasePropertyNamesContractResolver to CamelCase a string?我可以使用 CamelCasePropertyNamesContractResolver 来对字符串进行 CamelCase 吗?
【发布时间】:2015-12-09 10:26:09
【问题描述】:

有没有办法重用 CamelCasePropertyNamesContractResolver 将属性名称转换为字符串的方法?

我使用 CamelCasePropertyNamesContractResolver 作为 WebApi 的 JsonFormatter,效果很好。在我的应用程序的另一个区域,我将一些 .NET 模型属性动态转换为要在 JS 中使用的字符串。但是字符串是 Pascal 大小写(我的 .NET 标准也是如此),因此在 JS 中不能用于直接定位已格式化的 JS 对象属性。

// JS Example
var myJsonObject = getJsonObjectFromServer();
var myPropertyString = getPropertyNameFromServer();
var value = myJsonObject[myPropertyString];
// But value cannot be determined as casing of myPropertyString has changed

我想避免创建自己的 ToCamelCase 方法,该方法可能会创建不同的字符串。我还想继续使用 CamelCasePropertyNamesContractResolver(问题的根源),因为应用程序现在非常大,并且需要大量 JS 更新并使我的 js 不标准。

【问题讨论】:

    标签: .net json asp.net-web-api2


    【解决方案1】:

    http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Serialization_CamelCasePropertyNamesContractResolver.htm找到API

    public string JsonPropertyName
    {
        get
        {
            var resolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
            return resolver.GetResolvedPropertyName(PropertyName);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-27
      • 2016-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多