遵循 Ruby 属性命名约定的 C# JSON.NET 约定? [英] C# JSON.NET convention that follows Ruby property naming conventions?

查看:44
本文介绍了遵循 Ruby 属性命名约定的 C# JSON.NET 约定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 http://json.codeplex.com/ 并且我正在与基于 Ruby 的休息API.问题是大多数属性都有一个 ruby​​ 下划线命名约定.我想知道是否有人知道一种方法,这样我就可以避免添加大量 JsonProperty.

I am using http://json.codeplex.com/ and I am talking to a Ruby based Rest API. Problem is that most of the properties have a ruby underscore naming convention. I am wondering if anyone knows of a way so that I can avoid having to Add lots of JsonProperty.

例如,我想避免添加 JsonProperty 属性并在序列化程序设置中内置了一个约定,以便它知道尝试将带有下划线的属性映射到 .NET 命名约定:)

For example I want to avoid adding the JsonProperty attribute and have a convention built into the serializer settings so that it knows to try and map properties with an underscore in the to the .NET naming convention :)

public class Member
{
    [JsonProperty(PropertyName = "avatar_url")]
    public string AvatarUrl { get; set; }

    [JsonProperty(PropertyName = "twitter_screen_name")]
    public string TwitterScreenName { get; set; }

    [JsonProperty(PropertyName = "website_url")]
    public string WebSiteUrl { get; set; }
}

推荐答案

更新 - 2016 年 9 月:

Json.NET 9.0.1 具有 SnakeCaseNamingStrategy.您可以使用它来自动拥有 twitter_screen_name 样式属性.

Json.NET 9.0.1 has SnakeCaseNamingStrategy. You can use that to have twitter_screen_name style properties automatically.

DefaultContractResolver 继承并覆盖 ResolvePropertyName 以根据需要格式化属性名称.

Inherit from DefaultContractResolver and override ResolvePropertyName to format property names as you'd like.

CamelCasePropertyNamesContractResolver 对属性名称进行了类似的全局更改.

CamelCasePropertyNamesContractResolver does a similar global change to property names.

这篇关于遵循 Ruby 属性命名约定的 C# JSON.NET 约定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆